#!/user/bin/env python3
'oop2'
author = 'list' class Screen(object): @property def width(self): return self._width @width.setter def width(self,width): if isinstance(width,int): if width > 0: self._width = width else: raise ValueError('width > 0') else: raise TypeError('width must be int')
@property def height(self): return self._height @height.setter def height(self,height): if isinstance(height,int): if height > 0: self._height = height else: raise ValueError('height > 0') else: raise TypeError('height must be int') @property def resolution(self): return self._width * self._height
s = Screen() s.width = 1024 s.height = 768 print(s.resolution) assert s.resolution == 786432, '1024 * 768 = %d ?' % s.resolution
Sign in to make a reply
小新交作业
#!/user/bin/env python3
-- coding: utf-8 --
'oop2'
author = 'list' class Screen(object): @property def width(self): return self._width @width.setter def width(self,width): if isinstance(width,int): if width > 0: self._width = width else: raise ValueError('width > 0') else: raise TypeError('width must be int')
s = Screen() s.width = 1024 s.height = 768 print(s.resolution) assert s.resolution == 786432, '1024 * 768 = %d ?' % s.resolution