定義類的變量后茂浮,在類的方法中引用此變量時辙浑,寫法為:類名.變量名
代碼如下:
class C1():
count = 0 #定義類變量
def __init__(self,name):
self.name = name
C1.count +=1 ?#在類方法中引用此變量升略,寫法為:類名.變量名
def displaythename(self):
print "the name is",self.name
def dispalythecount(self):
print "the count is %d" % C1.count #在類方法中引用此變量,寫法為:類名.變量名
I1 = C1('bob')
I2 = C1('MEL')
I1.displaythename()
I2.displaythename()
print "the count is %d" % C1.count