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.countryp = People('aodaliya')print(p.getCountry()) #可以用过实例对象引用 # 同名方法时,类方法会覆盖实例方法# print(People.getCountry()) #可以通过类对象引用
------- 知识无价,汗水有情,如需搬运请注明出处,谢谢!
版权声明
本文仅代表作者观点,不代表百度立场。
本文系作者授权百度百家发表,未经许可,不得转载。




