九州体育网 - 十年信誉网站

关于作者

Discuss / Python / 作业,变量前面加__好像也可以,不知道跟_有什么区别

作业,变量前面加__好像也可以,不知道跟_有什么区别

Topic source

Gin阿金

#1 Created at ...

class Screen(object):

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

@width.setter
def width(self, value):
    if not isinstance(value, (int,float)):
        raise ValueError('Value is not legle')
    if value <= 0:
        raise ValueError('Value must > 0')
    else:           
        self.__width = value

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

@height.setter
def height(self, value):
    if not isinstance(value, (int,float)):
        raise ValueError('Value is not legle')
    if value <= 0:
        raise ValueError('Value must > 0')
    else:    
        self.__height = value

@property
def resolution(self):
    return self.__width*self.__height

测试1:

s = Screen() s.width = '2212' s.height = 0 print('resolution =', s.resolution) if s.resolution == 786432: print('测试通过!') else: print('测试失败!')

Value is not legle

测试2:

s = Screen() s.width = 0 s.height = 123 print('resolution =', s.resolution) if s.resolution == 786432: print('测试通过!') else: print('测试失败!')

Value must > 0

测试3:

s = Screen() s.width = 1024 s.height = 768 print('resolution =', s.resolution) if s.resolution == 786432: print('测试通过!') else: print('测试失败!')

resolution = 786432 测试通过!


  • 1

Reply

WARNING: You are using an old browser that does not support HTML5. Please choose a modern browser (Chrome / Microsoft Edge / Firefox / Sarafi) to get a good experience.