天天看點

類方法和執行個體方法同名,執行個體調用                                                                   -------  知識無價,汗水有情,如需搬運請注明出處,謝謝!

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

  

                                                                   -------  知識無價,汗水有情,如需搬運請注明出處,謝謝!

繼續閱讀