@property的应用

Posted Mr.H

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了@property的应用相关的知识,希望对你有一定的参考价值。

请利用@property给一个Screen对象加上widthheight属性,以及一个只读属性resolution

class Screen(object):
    @property
    def width(self):
        return self._width
    @width.setter
    def width(self,value):
        self._width=value
    @property
    def high(self):
        return self._high
    @high.setter
    def high(self,value):
        self._high=value
    @property
    def resolution(self):
        return self._high * self._width
        
s = Screen()
s.width = 1024
s.high = 768
print(resolution =, s.resolution)
if s.resolution == 786432:
    print(测试通过!)
else:
    print(测试失败!)

 

以上是关于@property的应用的主要内容,如果未能解决你的问题,请参考以下文章