python的静态,组态,继承3-静态方法
程序员文章站
2022-06-08 15:41:16
...
class Room:
tag = 1
def __init__(self, name, owner, width, length, heigh):
self.name = name
self.owner = owner
self.width = width
self.length = length
self.heigh = heigh
@property # 跟实例绑定
def cal_area(self):
# print('%s 住的 %s 总面积是%s' % (self.owner,self.name, self.width * self.length))
return self.width * self.length
@classmethod # 类的装饰器
def tell_info(cls, x):
print(cls)
print('--》', cls.tag, x) # print('--》',Room.tag)
# def tell_info(self):
# print('---->',self.tag)
@staticmethod # 类的工具包
def wash_body(a, b, c):
print('%s %s %s正在洗澡' % (a, b, c))
def test(x, y):
print(x, y)
Room.wash_body(‘alex’, ‘lzp’, ‘yxm’)
r1 = Room(‘厕所’, ‘alex’, 100, 100, 100000) # 实例化
r1.wash_body(‘alex’, ‘lzp’, ‘yxm’)
Room.test(1, 2)
r1.test(1,2)#不能使用,r1会把自己传进去与1,2冲突
实例化只有数据属性,没有函数属性
静态方法只是类的工具包
class Room:
tag = 1
def __init__(self, name, owner, width, length, heigh):
self.name = name
self.owner = owner
self.width = width
self.length = length
self.heigh = heigh
@property # 跟实例绑定
def cal_area(self):
# print('%s 住的 %s 总面积是%s' % (self.owner,self.name, self.width * self.length))
return self.width * self.length
@classmethod # 类的装饰器
def tell_info(cls, x):
print(cls)
print('--》', cls.tag, x) # print('--》',Room.tag)
# def tell_info(self):
# print('---->',self.tag)
@staticmethod # 类的工具包
def wash_body(a, b, c):
print('%s %s %s正在洗澡' % (a, b, c))
def test(x, y):
print(x, y)
Room.wash_body('alex', 'lzp', 'yxm')
r1 = Room('厕所', 'alex', 100, 100, 100000) # 实例化
r1.wash_body('alex', 'lzp', 'yxm')
Room.test(1, 2)
# r1.test(1,2)#不能使用,r1会把自己传进去与1,2冲突