Discuss / Python / homework

homework

Topic source
class Screen(object):

	@property
	def width(self):
		return self._width

	@width.setter
	def width(self, width):
		if not isinstance(width, int):
			raise ValueError('宽度必须是数字!')
		self._width = width

	@property
	def height(self):
		return self._height

	@height.setter
	def height(self, height):
		if not isinstance(height, int):
			raise ValueError('高度必须是数字!')
		self._height = height

	@property
	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('测试失败!')
	
	

	

  • 1

Reply