‘’’
通过类方法修改类属性
‘’’
class Person(object):
country = ‘中国’
@classmethod
def countryinfo(cls):
pass
@classmethod
def changecountry(cls,newcountry):
cls.country = newcountry
print(‘此时的国籍是:’,cls.country)
def init(self,name,age,gender):
self.name = name
self.age = age
self.gender = gender
if name == ‘main’:
# 通过类对象进行调用
Person.countryinfo()
Person.changecountry(‘USA’)
# 通过实例对象进行调用
zhang = Person
Person.countryinfo()
Person.changecountry(‘法国’)