python 静态成员变量
python 也可以定义静态成员变量,就是类的变量。
注意python的静态成员变量只能通过 类.变量名 的形式获取
废话不多了,看代码吧
1 class A: 2 a1 = 0 3 def __init__(self, a2): 4 self.a2 = a2 5 6 def setData(self, a3): 7 self.a3 = a3 8 9 def show(self): 10 print 'A1:%s,a1:%s,a2:%s,a3:%s' %(A.a1, self.a1, self.a2, self.a3) 11 12 if __name__ == '__main__': 13 obj1 = A(1) 14 obj2 = A(2) 15 obj3 = A(3) 16 17 obj1.setData(1) 18 obj2.setData(2) 19 obj3.setData(3) 20 21 obj1.a1 = 1 22 obj2.a1 = 2 23 obj3.a1 = 3 24 25 A.a1 = 1 26 A.a2 = 2 27 A.a3 = 3 28 obj1.show() 29 obj2.show() 30 obj3.show() 31 print 'A1:%s,A2:%s,A3:%s' %(A.a1, A.a2, A.a3)
输出结果:
A1:1,a1:1,a2:1,a3:1
A1:1,a1:2,a2:2,a3:2
A1:1,a1:3,a2:3,a3:3
A1:1,A2:2,A3:3
posted on 2012-06-28 11:16 NashZhou 阅读(32023) 评论(1) 编辑 收藏 举报