@
目录
1.说明
python中属性:类属性,实例属性
方法:类方法,实例方法,静态方法
想修改类属性,只能是类方法,因为只有类方法把cls(类)传入数据里面
静态方法也就是个普通的方法,为了方便而已
实例方法,不能通过类来直接调用,要调用也可以self = 对象名
具体下面
2.代码
class Provice(object):
#类属性
country = "china" def __init__(self,name):
#实例属性
self.name = name def self_control(self):
print("我是实例方法,我必须有self",self.name) @staticmethod
def static_method():
#由类调用,无默认参数
print("我是static_method") @classmethod
def class_method(cls): print("我是classmethod++",cls.country)
cls.country = "china___"sichuan = Provice("sichuan")
print(sichuan.name,sichuan.country,Provice.country)
Provice.class_method()
Provice.static_method()
Provice.self_control(sichuan)
sichuan.static_method()print(sichuan.name,sichuan.country,Provice.country)
关于作者
个人公众号: