1.请利用@property给一个Screen对象加上width和height属性,以及一个只读属性resolution:
#!/usr/bin/env python3
#-*-coding=utf-8-*-
'a test module'
#请利用@property给一个Screen对象加上width和height属性,以及一个只读属性resolution:
__author__='gkm'
class Screen(object):
@property
def width(self):
return self._width
@width.setter #这里是width.setter
def width(self,width):
if not isinstance(width,int):
raise ValueError('not int')
else:
self._width = width
@property
def height(self):
return self._height
@height.setter
def height(self,height):
if not isinstance(height,int):
raise ValueError('not int')
else:
self._height = height
@property #只读的相当于只getter,get可以不写
def resolution(self):
return self._width * self._height
s = Screen()
s.width = 1024
s.height = 768
print('resolutin=',s.resolution)
if s.resolution == 786432:
print('ok')
else:
print('false')