python class 类方法的实现以及类方法的使用

‘’’
类方法的实现
‘’’
class Person(object):
‘’’
创建Person类,属性有名字,年龄,性别,创建personInfo方法,打印信息
‘’’
country = ‘中国’#类属性
@classmethod #标注类方法
def countryinfo(cls):
pass
def init(self,name,age,gender):
self.name = name
self.age = age
self.gender = gender
“参数书写问题,第一个参数一般写cls,”
“加上装饰器,@classmethod标注为类方法”

if name == ‘main’:
#类方法的使用
Person.countryinfo()#通过类对象进行调用
wang = Person(‘xiaoxiao’,18,‘girl’)#通过实例对象进行调用
wang.countryinfo()

猜你喜欢

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