静态方法只是名义上的归属类管理,不能使用类变量和实例变量,是类的工具包
1 class Room: 2 def __init__(self, name, owner, width, length, heigh): 3 self.name = name 4 self.owner = owner 5 self.width = width 6 self.length = length 7 self.heigh = heigh 8 9 @property # 函数属性变为数据属性,调用方式改变 10 def cal_area(self): 11 # print('%s 住的 %s 总面积是%s' % (self.owner, self.name, self.width * self.length)) 12 return self.width * self.length 13 14 @property 15 def cal_total(self): 16 return self.width * self.length * self.heigh 17 18 @classmethod # 表示这是类方法,默认传递类本身。通过类名.函数名 19 def tell_info(cls): 20 print(cls) 21 print('你好,这是类方法') 22 23 @staticmethod 24 def wash_body(a, b, c): 25 print('%s %s %s 在洗澡' % (a, b, c)) 26 27 def test(): 28 print('我什么都不绑定') 29 30 31 Room.wash_body(1, 2, 3) 32 p1 = Room('小白', '哈哈', 100, 10, 1250) 33 p1.wash_body(1, 2, 3) 34 Room.test() 35 # p1.test() # 将实例自动传进去所以报错 36 输出: 37 1 2 3 在洗澡 38 1 2 3 在洗澡 39 我什么都不绑定