class Animal(object):
__count=0
def __init__(self,name,age,gender):
self.name=name
self.age=age
self.gender=gender
@classmethod
def set_count(cls):
cls.__count+=1
@classmethod
def get_count(cls):
return cls.__count
dog=Animal('littledog',2,'male')
Animal.set_count()
cat=Animal('littlecat',3,'female')
Animal.set_count()
print(Animal.get_count())
def __init__(self,name,age,gender):
self.name=name
self.age=age
self.gender=gender
这几行的缩进,比@classmethod的缩进多一个空格
代码没问题,注意缩进即可
class Animal(object):
__count=0
def __init__(self,name,age,gender):
self.name=name
self.age=age
self.gender=gender
@classmethod
def set_count(cls):
cls.__count+=1
@classmethod
def get_count(cls):
return cls.__count
dog=Animal('littledog',2,'male')
Animal.set_count()
cat=Animal('littlecat',3,'female')
Animal.set_count()
慕勒4078882,请标识下哪儿缩进不对,请标识下,谢谢了
你class里缩进不对,没完全对齐。
# Enter a code
class Animal(object):
__count=0
def __init__(self,name,age,gender):
self.name=name
self.age=age
self.gender=gender
@classmethod
def set_count(cls):
cls.__count+=1
@classmethod
def get_count(cls):
return cls.__count
dog=Animal('littledog',2,'male')
Animal.set_count()
cat=Animal('littlecat',3,'female')
Animal.set_count()
print(Animal.get_count())