python_101_类方法

class Dog(object):
    n=333
    name='小虎子'
    def __init__(self,name):
        self.name=name
    @classmethod
    def eat(self,food):
        print('n=%s'%self.n)
        print('%s is eating %s'%(self.name,food))
    def talk(self):
        print('%s is fully'%self.name)
d1=Dog('白蹄')
d1.eat('肉')
d1.talk()
#类方法:只能访问类变量,不能访问静态变量
#http://www.cnblogs.com/alex3714/articles/5213184.html

  结果:

n=333
小虎子 is eating 肉
白蹄 is fully
View Code

 

posted @ 2018-03-02 22:19  耐烦不急  阅读(259)  评论(0编辑  收藏  举报