@classmethod

发布时间:2019-03-16 22:24:09编辑:auto阅读(1962)

    代码部分:

    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

关键字