天天看點

類方法和執行個體方法同名,執行個體調用

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())    #可以通過類對象引用