定义类的变量后,在类的方法中引用此变量时,写法为:类名.变量名
代码如下:
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