1、实现内容:使用@property给对象Screen加上width和height属性,以及一个只读属性resolution
2-1:错误1(此处纯属脑残):
Traceback (most recent call last):
File "F:\w26511\python练习\test.py", line 43, in <module>
s.width = 1024
AttributeError: can't set attribute
# -*- coding: utf-8 -*-
class Screen(object):
#定义width属性
@property
def width(self):
return self.width
@width.setter
def width(self,value):
if not isinstance(value,int):
raise ValueError('宽度必须是数字')
elif value<=0:
raise ValueError('宽度必须是正数')
else:
self.width=value
@property
def height(self):
return self.height
@height.setter
def height(self,value):
if not isinstance(value,int):
raise ValueError('宽度必须是数字')
elif value<=0:
raise ValueError('宽度必须是正数')
else:
self.height=value
@property