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())    #可以通过类对象引用

  

相关文章:

  • 2021-11-17
  • 2022-12-23
  • 2022-12-23
  • 2021-09-17
  • 2022-02-03
  • 2022-12-23
  • 2022-01-30
猜你喜欢
  • 2022-12-23
  • 2021-12-02
  • 2022-12-23
  • 2021-05-24
  • 2021-07-15
  • 2022-12-23
相关资源
相似解决方案