矩形类
| 10 | |
| 11 | |
| 12 | class Rect(object): |
| 13 | """矩形类""" |
| 14 | |
| 15 | def __init__(self, width=0, height=0): |
| 16 | """构造器""" |
| 17 | self.__width = width |
| 18 | self.__height = height |
| 19 | |
| 20 | def perimeter(self): |
| 21 | """计算周长""" |
| 22 | return (self.__width + self.__height) * 2 |
| 23 | |
| 24 | def area(self): |
| 25 | """计算面积""" |
| 26 | return self.__width * self.__height |
| 27 | |
| 28 | def __str__(self): |
| 29 | """矩形对象的字符串表达式""" |
| 30 | return '矩形[%f,%f]' % (self.__width, self.__height) |
| 31 | |
| 32 | def __del__(self): |
| 33 | """析构器""" |
| 34 | print('销毁矩形对象') |
| 35 | |
| 36 | |
| 37 | if __name__ == '__main__': |