使用@property作业
1 #!/usr/bin/env python3 2 #-*- coding:utf-8 -*- 3 class Screen(object): 4 __slots__ = ('_width','_height') 5 def __init__(self, *args, **kwargs): 6 self._width = 800 7 self._height = 600 8 9 @property 10 def width(self): 11 return self._width 12 13 @width.setter 14 def width(self,value): 15 self._width = value 16 17 @property 18 def height(self): 19 return self._height 20 21 @height.setter 22 def height(self,value): 23 self._height = value 24 25 @property 26 def resolution(self): 27 return self._width*self._height 28 29 # test: 30 s = Screen() 31 s.width = 1024 32 s.height = 768 33 print(s.resolution) 34 assert s.resolution == 786432, '1024 * 768 = %d ?' % s.resolution
教程地址: