使用@property作业

 1 #!/usr/bin/env python3
 2 #-*- coding:utf-8 -*-
 3 class Screen(object):
 4     __slots__ = ('_width','_height')
 5     def __init__(self, *args, **kwargs):
 6         self._width = 800
 7         self._height = 600
 8 
 9     @property
10     def width(self):
11         return self._width
12 
13     @width.setter
14     def width(self,value):
15         self._width = value
16 
17     @property
18     def height(self):
19         return self._height
20 
21     @height.setter
22     def height(self,value):
23         self._height = value
24     
25     @property
26     def resolution(self):
27         return self._width*self._height
28 
29 # test:
30 s = Screen()
31 s.width = 1024
32 s.height = 768
33 print(s.resolution)
34 assert s.resolution == 786432, '1024 * 768 = %d ?' % s.resolution    

教程地址:

https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/00143186781871161bc8d6497004764b398401a401d4cce000

posted @ 2017-08-26 23:16  绝望的老猫  阅读(151)  评论(0编辑  收藏  举报