-
class Person(object):
def __init__(self, name, gender, **kw):
self.name = name
self.gender = gender
for k, v in kw.items():
setattr(self, k, v)
p = Person('Bob','Male',age=18,course='Python')
print(p.age)
print(p.course)
查看全部 -
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
class SkillMixin(object):
def __init__():
pass
class BasketballMixin(SkillMixin):
def __init__():
pass
class FootballMixin(SkillMixin):
def __init__():
pass
class Student_Basketball(Student,BasketballMixin):
def __init__(self,name,gender,score):
super(Student_Basketball,self).__init__(name,gender,score)
class Teacher_Football(Teacher,FootballMixin):
def __init__(self,name,gender,course):
super(Teacher_Football,self).__init__(name,gender,course)
查看全部 -
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', 88)
t = Teacher('Alice', 'Female', 'English')
print(isinstance(t,Person))
print(isinstance(t,Student))
print(isinstance(t,Teacher))
查看全部 -
class Person(object):
def __init__(self, name, gender):
self.name = name
self.gender = gender
class Teacher(Person):
def __init__(self,name,gender,subject):
super(Teacher,self).__init__(self,name,gender)
self.subject = subject
查看全部 -
class Animal():
__count = 0
@classmethod
def get_count(cls):
return cls.__count
def __init__(self, name, age, localtion):
self.__name = name
self.__age = age
self.__localtion = localtion
Animal.__count += 1
def get_name(self):
return self.__name
def get_age(self):
return self.__age
def get_localtion(self):
return self.__localtion
def set_name(self,name):
self.__name = name
return
def set_age(self,age):
self.__age = age
return
def set_localtion(self,localtion):
self.__localtion = localtion
return
def print_info(self):
print('The name is : ' + self.__name)
print('The age is :' + str(self.__age))
print('The localtion is : ' + self.__localtion)
dog = Animal('wangcai', 3, 'Beijing')
print(Animal.get_count())
dog.print_info()
查看全部 -
class Animal():
def __init__(self, name, age, localtion):
self.__name = name
self.__age = age
self.__localtion = localtion
def get_name(self):
return self.__name
def get_age(self):
return self.__age
def get_localtion(self):
return self.__localtion
def set_name(self,name):
self.__name = name
return
def set_age(self,age):
self.__age = age
return
def set_localtion(self,localtion):
self.__localtion = localtion
return
def print_info(self):
print('The name is : ' + self.__name)
print('The age is :' + str(self.__age))
print('The localtion is : ' + self.__localtion)
dog=Animal('wangcai',3,'Beijing')
dog.print_info()
查看全部 -
class Animal():
__count = 0
__age = 0
def __init__(self,name,age):
self.name = name
self.__age = age
self.__count += 1
dog=Animal('wangcai',3)
print(dog.age)
查看全部 -
class Animal():
__count = 0
def __init__(self,name,age):
self.name = name
self.age = age
Animal.__count += 1
dog=Animal('wangcai',3)
print(Animal.__count)
print(dog.__count)
查看全部 -
class Animal():
_count = 0
def __init__(self,name,age):
self.name = name
self.age = age
Animal._count += 1
dog=Animal()
print(Animal._count)
print(dog._count)
查看全部 -
class Animal():
count = 0
def __init__(self,name,age):
self.name = name
self.age = age
Animal.count += 1
dog = Animal('wangcai',5)
print(dog.count)
cat = Animal('mimi',3)
print(dog.count,cat.count)
print(dog.name,dog.age)
print(cat.name,cat.age)
查看全部 -
class Animal():
def __init__(self,name,age):
self.name = name
self.age = age
dog = Animal('wangcai',5)
cat = Animal('mimi',3)
print(dog.name,dog.age)
print(cat.name,cat.age)
查看全部 -
class Animal():
pass
dog = Animal()
cat = Animal()
dog.name='wangcai'
cat.name='mimi'
dog.age=5
cat.age=3
print(dog.name,dog.age)
print(cat.name,cat.age)
查看全部 -
class Animal():
pass
dog = Animal()
cat = Animal()
print(dog)
print(cat)
查看全部 -
class Animal:
__count = 0
def __init__(self,name):
self.name = name
Animal.__count += 1 #类的实例数量
dog = Animal("wangwang")
cat = Animal("mimi")
bird = Animal("jiji")
print(Animal.__count)In the given Python code, the line Animal.count += 1 is using the class name Animal rather than self for incrementing the count variable. This is because count is a class variable, shared by all instances of the Animal class.
When you use Animal.count, you are directly accessing the class variable and incrementing its value. This means that every time a new instance of the Animal class is created, the count variable is incremented for the entire class, not just for the specific instance.
On the other hand, if you were to use self.count += 1 in the __init__ method, it would create an instance variable count specific to each instance of the class. In that case, each instance would have its own separate count value.
class Animal:
count = 0
def __init__(self,name):
self.name = name
self.count += 1
dog = Animal("wangwang")
cat = Animal("mimi")
bird = Animal("jiji")
print(dog.count)#独立计算每个实例都个数,都是1print(Animal.count)#类变量为0
查看全部 -
class Animal:
__count = 0
def __init__(self,name):
self.name = name
Animal.__count +=1
@classmethod
def get_count(cls):
return cls.__count
dog = Animal("wangwang")
cat = Animal("mimi")
print(Animal.get_count())查看全部
举报