Discuss / Python / 作业

作业

Topic source

class Screen:

def __init__(self):

self._width = None

self._height = None

@property

def width(self):

return self._width

@width.setter

def width(self, value):

if not isinstance(value, (int, float)):

raise ValueError("width must be int or float!")

if value < 0:

raise ValueError("width must be greater 0!")

self._width = value

@property

def height(self):

return self._height

@height.setter

def height(self, value):

if not isinstance(value, (int, float)):

raise ValueError("height must be int or float!")

if value < 0:

raise ValueError("height must be greater 0!")

self._height = value

@property

def resolution(self):

return f"{self._width} * {self._height}"

sc = Screen()

sc.width = 1080

sc.height = 1920

print(sc.resolution)  # 1080 * 1920

sc.width = 720

sc.height = 1020

print(sc.resolution)  # 720 * 1020

sc1 = Screen()

sc1.width = 1024

sc1.height = 768

print('resolution =', sc1.resolution)

if sc1.resolution == "1024 * 768":

    print('测试通过!')

else:

    print('测试失败!')


  • 1

Reply