码迷,mamicode.com
首页 > 其他好文 > 详细

【面向对象】类方法

时间:2018-05-22 22:02:11      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:wan   ati   SM   style   实例   method   ini   nbsp   __init__   

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

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 骨头

 

【面向对象】类方法

标签:wan   ati   SM   style   实例   method   ini   nbsp   __init__   

原文地址:https://www.cnblogs.com/q1ang/p/9074013.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!