class Screen(object): @property def width(self): return self._width
@width.setter def width(self,value): if isinstance(value,int) and value >= 0: self._width = value else: raise ValueError('width只能为正整数') @property def height(self): return self._height @height.setter def height(self,value): if isinstance(value,int) and value >= 0: self._height = value else: raise ValueError('height只能为正整数') @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
我用的方法是在每行代码前面加个TAB
Sign in to make a reply
skysmine
-- coding: utf-8 --
class Screen(object): @property def width(self): return self._width
test:
s = Screen() s.width = 1024 s.height = 768 print(s.resolution) assert s.resolution == 786432, '1024 * 768 = %d ?' % s.resolution