1、=======不可修改变量值方法:=============
class Rectangle(object):
def __init__(self):
self.true_width=1036
self.true_height=777
@property
def width(self):
# 变量名不与方法名重复,改为true_width,通过此方法可以防止值被随意修改
return self.true_width
@property
def height(self):
return self.true_height
s = Rectangle()
# 加property后可以像变量一样直接调用方法名,不需要再加括号
#s.width = 1096 @property代表私有属性, 不能直接赋值,否则会报错
#s.height = 773 @property代表私有属性, 不能直接赋值,否则会报错
s.true_width=1096
print(s.width,s.height) 1096 777
2、=====可修改变量值方法:==========
class Rectangle(object)