类方法和实例方法同名,实例调用

class People(object):
    country = 'china'
    def __init__(self,name):
        self.country = name
    def getCountry(self):          # -- 实例方法
        return self.country
    #类方法,用classmethod来进行修饰
    @classmethod
    def getCountry(cls):           # -- 类方法
        return cls.country

p = People('aodaliya')
print(p.getCountry())    #可以用过实例对象引用         # 同名方法时,类方法会覆盖实例方法
# print(People.getCountry())    #可以通过类对象引用

  

posted @ 2018-05-08 17:05  汪凡  阅读(1029)  评论(1编辑  收藏  举报