Discuss / Python / 123

class Screen(object):

    def __init__(self):

        self._height = None

        self._width = None

    @staticmethod

    def _validate(v: int) -> bool:

        if isinstance(v, int) and v > 0:

            return True

        return False

    @property

    def width(self):

        return self._width

    @width.setter

    def width(self, v: int):

        if self._validate(v):

            self._width = v

        else:

            raise ValueError('不合法的宽度')

    @property

    def height(self):

        return self._height

    @height.setter

    def height(self, v: int):

        if self._validate(v):

            self._height = v

        else:

            raise ValueError('不合法的高度')

    @property

    def resolution(self):

        if isinstance(self._height, int) and isinstance(self._width, int):

            return self._width * self._height

        else:

            raise AttributeError('宽高参数有缺失')

s = Screen()

s.width = 1024

s.height = 768

print('resolution =', s.resolution)

if s.resolution == 786432:

    print('测试通过!')

else:

    print('测试失败!')


  • 1

Reply