python class 通过类方法修改类属性

‘’’
通过类方法修改类属性
‘’’
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(‘法国’)

在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44737399/article/details/89082534