Discuss / Python / 练习练习,property 好用

练习练习,property 好用

Topic source

这次的作业:

class Screen(object):
    def __init__(self):
        self._width = 0
        self._height = 0

    @property
    def width(self):
        return self._width

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

    @width.setter
    def width(self,value):
        if not isinstance(value,int):
            raise ValueError('score must be an integer!')
        if value<= 0:
            raise ValueError('int must "大于零"')
        self._width = value

    @height.setter
    def height(self,value):
        if not isinstance(value,int):
            raise ValueError('score must be an integer!')
        if value<= 0:
            raise ValueError('int must "大于零"')
        self._height= value

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

好详细,增加了合法性检查逻辑,学习了。

樊小明RR

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

膜拜。


  • 1

Reply