代码部分:
class Dog(object):
name="huazi"
def __init__(self,name):
self.name=name
@classmethod
def eat(self):
print("%s is eating %s"%(self.name,"a"))
def talk(self):
print("%s is talking"%self.name)
d=Dog("erha")
d.eat()
d.talk()
执行结果:
huazi is eating a
erha is talking
结论:@classmethod下的类方法eat()只能调用类变量name