-
# Enter a code
name ='BOB'
ka =88
print('%s score is %i'%(name,ka))
查看全部 -
输出变量类型
查看全部 -
# Enter a code
class person(object):
def __init__(self,name,gender):
self.name = name
self.gender = gender
def who(self):
return 'i am a person , my name is %s'%self.name
class student(person):
def __init__(self,name,gender,score):
super(student,self).__init__(name,gender)
self.score = score
def who(self):
return 'i am a student, my name is %s'% self.name
class teacher(person):
def __init__(self,name,gender,course):
super(teacher,self).__init__(name,gender)
self.course = course
def who(self):
return 'i am a teacher, my name is %s'%self.name
p= person('ok','male')
s =student('BN','male',88)
t = teacher('alice','female','english')
print (p.who())
print(s.who())
print (t.who())
class skm(object):
def __init__(self,name):
self.name = name
def who(self):
return ' i can paluy'
class baskm(skm,student):
def __init__(self,name,course):
super(baskm,self).__init__(name)
self.course = course
def who(self):
return ' yes, i am student, i can play '+self.course
class fbskm(skm,student):
def __init__(self,name,gender,course):
super(baskm,self).__init__(name,gender,course)
self.course = course
def who(self):
return ' yes, i am student, i can play football!'
p1= baskm('bob','basketball')
print (p1.who())
查看全部 -
# Enter a code
class person(object):
def __init__(self,name,gender):
self.name = name
self.gender = gender
def who(self):
return 'i am a person , my name is %s'%self.name
class student(person):
def __init__(self,name,gender,score):
super(student,self).__init__(name,gender)
self.score = score
def who(self):
return 'i am a student, my name is %s'% self.name
class teacher(person):
def __init__(self,name,gender,course):
super(teacher,self).__init__(name,gender)
self.course = course
def who(self):
return 'i am a teacher, my name is %s'%self.name
p= person('ok','male')
s =student('BN','male',88)
t = teacher('alice','female','english')
print (p.who())
print(s.who())
print (t.who())
class skm(object):
def __init__(self,name):
self.name = name
return 'YES, I can!'
class baskm(skm):
def __init__(self,name)
查看全部 -
# coding=utf-8
class person(object):
def __init__(self,name,gender):
self.name = name
self.gender = gender
class student(person):
def __init__(self,name,gender,score):
super(student,self).__init__(name,gender)
self.score = score
class teacher(person):
def __init__(self,name,gender,course):
super(teacher,self).__init__(name,gender)
self.course = course
p = person('tim','male')
s = student('bob','male',99)
t = teacher('alice','famale','english')
x =isinstance(p,person)
y = isinstance(p, teacher)
print x
print y
#super
#def
#coding = utf-8
查看全部 -
# coding=utf-8
class person(object):
def __init__(self,name,gender):
self.name = name
self.gender = gender
class student(person):
def __init__(self,name,gender,score):
super(student,self).__init__(name,gender)
self.score = score
class teacher(person):
def __init__(self,name,gender,course):
super(teacher,self).__init__(name,gender)
self.course = course
p = person('time','male')
s = student('bob','boy',66)
t = teacher('ale',)
查看全部 -
# Enter a code
class person(object):
_location ='aisa'
def __init__(self,name,age,gender):
self.name = name
self.age =age
self.gender = gender
class student(person):
def __init__(self, name,age,gender,score):
super(student, self).__init__(name,age,gender)
self.score = score
st = student('wangwang',12,'123',100)
print(st.name)
person类中的OBJECT 是必须的, 否则报错
详见
https://www.cnblogs.com/likeshan168/articles/3558939.html
查看全部 -
请给 Animal类添加一个类属性 count,每创建一个实例,count 属性就加 1,这样就可以统计出一共创建了多少个 Animal的实例。
# Enter a code class Animal(object): count = 0 def __init__(self, name, age): Animal.count += 1 self.name = name self.age = age dog = Animal('xiaohuang', 5) cat = Animal('mimi', 3) print Animal.count
##类属性也是可以动态添加和修改的,需要注意的是,因为类属性只有一份
查看全部 -
class person():
_location = 'aisa'
def __init__ (self,name,age):
self.name = name
self.age = age
def set_location(cls,location):
cls._location = location
def get_location(cls):
return cls._location
p = person('wabgwabg',16)
p.set_location('qwe111')
print p._location
xl = p.get_location()
print xl
查看全部 -
class person():
def __init__(self,name,age):
self.name = name
self.age = age
def get_name(self):
return self.name,self.age
p= person('wangwang',15)
xm = p.get_name()
print('name={}','age={}'.format(xm[1],xm[0]))
tuple list 为什么是读取不成功, 重新看下 tuple 与list。
查看全部 -
class Animal(object):
def __init__(self, name, age):
self.name = name
self._age = age
cat = Animal('Kitty', '3')
print(cat.name)
print(cat._age)
查看全部 -
class person():
def __init__(self,name,sex,age):
# two_ befor and behind init
self.name = name
self.sex = sex
self.age = age
xm = person('name','s',15)
print xm.name
查看全部 -
class animal():pass
dog = animal()
cat = animal()
dog.name ='wangwang'
cat.name ='miaomiao'
dog.age =4
cat.age =2
print dog.name
print cat.age
查看全部 -
__count的意思是把count改为私有,实例、类都无法直接在def外部访问__count,是俩横线。报错为没有__count这个属性。
查看全部 -
Animal.count+=1
是Animal.count=Animal.count+1的意思
查看全部
举报