类属性
类的属性有两类:实例属性、类属性。
1 class Demo_Property: 2 class_name = "Demo_Property" 3 4 def __init__(self,x=0): 5 self.x=x 6 7 def class_info(self): 8 print('类变量值:',Demo_Property.class_name) 9 print('实例变量值:',self.x) 10 11 def chng(self,x): 12 self.x=x 13 14 def chng_cn(self,name): 15 Demo_Property.class_name = name 16 dpa=Demo_Property() 17 dpb=Demo_Property() 18 print('初始化这两个实例') 19 dpa.class_info() 20 dpb.class_info() 21 print('修改实例变量') 22 print('修改dpa实例变量') 23 dpa.chng(3) 24 dpa.class_info() 25 dpb.class_info() 26 print('修改dpb实例变量') 27 dpb.chng(10) 28 dpa.class_info() 29 dpb.class_info() 30 print('修改类变量') 31 print('修改dpa类变量') 32 dpa.chng_cn('dpa') 33 dpa.class_info() 34 dpb.class_info() 35 print('修改dpb实例变量') 36 dpb.chng_cn('dpb') 37 dpa.class_info() 38 dpb.class_info()