class Screen(object): # def __init__(self, width, height): # self._width = width # self.height = height
@property # 可以将方法width 作为属性进行调用 def width(self): # 方法 widthreturn self._width # 实例变量 _width @width.setter # 可以对实例变量 _width 进行修改设置 def width(self, value): self._width = value
@property # 方法作属性调用 def height(self): # 方法 widthreturn self._height # 实例变量 _width @height.setter # 可以对实例变量 _height 进行修改设置 def height(self, value): self._height = value
@property # 可以将方法resolution 作为属性进行调用 def resolution(self): return self._width * self._height
# 测试: s = Screen() s.width = 1024 s.height = 768 print('resolution =', s.resolution) if s.resolution == 786432: print('测试通过!') else: print('测试失败!')
Sign in to make a reply
Afternoon Tea
class Screen(object):
# def __init__(self, width, height):
# self._width = width
# self.height = height
@property # 可以将方法width 作为属性进行调用
def width(self): # 方法 widthreturn self._width # 实例变量 _width
@width.setter # 可以对实例变量 _width 进行修改设置
def width(self, value):
self._width = value
@property # 方法作属性调用
def height(self): # 方法 widthreturn self._height # 实例变量 _width
@height.setter # 可以对实例变量 _height 进行修改设置
def height(self, value):
self._height = value
@property # 可以将方法resolution 作为属性进行调用
def resolution(self):
return self._width * self._height
# 测试:
s = Screen()
s.width = 1024
s.height = 768
print('resolution =', s.resolution)
if s.resolution == 786432:
print('测试通过!')
else:
print('测试失败!')