1 #类属性和实例属性 2 class Tool(object): 3 4 #类属性 5 total = 0 6 7 #静态方法 8 @classmethod 9 def say(self): 10 print("hello world") 11 12 def __init__(self,new_name): 13 #实例属性 14 self.name = new_name 15 #类属性 16 Tool.total += 1 17 18 tool1 = Tool("a1") 19 tool2 = Tool("a2") 20 tool3 = Tool("a3") 21 22 print(tool1.name) 23 print(tool2.name) 24 print(tool3.name) 25 26 print(Tool.total) 27 28 Tool.say()
打印结果:
a1
a2
a3
3
hello world