【Python】@property

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37701443/article/details/82728851

Describe

Answer

class Screen(object):
    @property
    def width(self):
        return self._width

    @width.setter
    def width(self, value):
        self._width = value

    @property
    def height(self):
        return self._height

    @height.setter
    def height(self, value):
        self._height = value

    @property
    def  resolution(self):
        return self._width*self._height

# test:
s = Screen()
s.width = 1024
s.height = 768
print(s.resolution)
assert s.resolution == 786432, '1024 * 768 = %d ?' % s.resolution
#assert 断言
# 正常情况下,这个结果应该是786432,如果结果不为786432 输出后面的语句 '1024 * 768 = %d ?

Output

786432

Process finished with exit code 0

猜你喜欢

转载自blog.csdn.net/qq_37701443/article/details/82728851