【面向对象】类方法

类方法:只能访问类变量,不能访问实例变量

class Dog(object):
    n='wangcai'
    def __init__(self,name):
        self.name=name

    @classmethod #类方法
    def eat(self):
        print('%s is eating %s'%(self.n,'骨头'))

d=Dog('旺财')

d.eat()
>>>wangcai is eating 骨头
原文地址:https://www.cnblogs.com/q1ang/p/9074013.html