Discuss / Python / 请各位大神批评指正,谢谢

请各位大神批评指正,谢谢

Topic source

中大-雪山

#1 Created at ... [Delete] [Delete and Lock User]

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 没有效果

soraixy

#3 Created at ... [Delete] [Delete and Lock User]

变量最好都加下划线 _width _height


  • 1

Reply