首先理解@property:
@property是将python定义的函数"当做"属性来访问,从而提供更加友好的访问的方式.
用法:
1.只有@property表示只读
2.同时有@property和@.setter表示可以读可以写
3.同时有@property和@.setter和@*.deleter表示可读可写可删除
class screen(object):
@property
def wide(self):
return self._wide
@wide.setter
def wide(self,wide2):
self._wide = wide2
@property
def height(self):
return self._height
@height.setter
def height(self,height2):
self._height = height2
@property
def resolution(self):
return self._wide*self._height
s = screen()
s.wide=1600
s.height = 768
print('wide*height = ',s.resolution)
结果
wide*height = 1228800
网友评论