python 静态成员变量

python 静态成员变量

 

python 也可以定义静态成员变量,就是类的变量。

注意python的静态成员变量只能通过 类.变量名 的形式获取

 

class A:
    a1 = 0
    def __init__(self, a2):
        self.a2 = a2

    def setData(self, a3):
        self.a3 = a3

    def show(self):
        print 'A1:%s,a1:%s,a2:%s,a3:%s' %(A.a1, self.a1, self.a2, self.a3)

if __name__ == '__main__':
    obj1 = A(1)
    obj2 = A(2)
    obj3 = A(3)

    obj1.setData(11)
    obj2.setData(22)
    obj3.setData(33)

    obj1.a1 = 7
    obj2.a1 = 8
    obj3.a1 = 9
    
    A.a1 = 4
    A.a2 = 5
    A.a3 = 6
    obj1.show()
    obj2.show()
    obj3.show()
    print 'A1:%s,A2:%s,A3:%s' %(A.a1, A.a2, A.a3)

输出结果为:

A1:4,a1:7,a2:1,a3:11
A1:4,a1:8,a2:2,a3:22
A1:4,a1:9,a2:3,a3:33
A1:4,A2:5,A3:6

 

posted @ 2016-03-25 13:17  静之深  阅读(1816)  评论(0编辑  收藏  举报