静态属性@property

property 作用其实把类里面的逻辑给隐藏起来(封装逻辑,让用户调用的时候感知不到你的逻辑)

property实例1:
class
Room:

def __init__(self):
pass

@property #将函数属性变成静态属性(后面调用的时候,就不需要用x.status()来调用了,直接x.status执行即可)
def status(self):
print('123')

R = Room()
R.status #结果打印 123

property实例2:
class Room:
def __init__(self):
pass

@property
def status(self):
return 123

R = Room()
print(R.status) #结果是返回 123


猜你喜欢

转载自www.cnblogs.com/ajaxa/p/9044810.html