class Screen(object): @property def trait(self, width, height): self.width = width self.height = height @trait.setter def trait(self, width, height): self.width = width self.height = 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
不晓得这样对不对,请各位大神指正
定义的width,height 都是public 可以在外部修改 所以你定义的@property 没有效果
变量最好都加下划线 _width _height
Sign in to make a reply
中大-雪山
class Screen(object):
@property def trait(self, width, height): self.width = width self.height = height @trait.setter def trait(self, width, height): self.width = width self.height = 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
不晓得这样对不对,请各位大神指正