CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
jackfrued

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: jackfrued/Python-100-Days
Path: blob/master/Day01-15/code/Day08/rect.py
Views: 729
1
"""
2
定义和使用矩形类
3
4
Version: 0.1
5
Author: 骆昊
6
Date: 2018-03-08
7
"""
8
9
10
class Rect(object):
11
"""矩形类"""
12
13
def __init__(self, width=0, height=0):
14
"""初始化方法"""
15
self.__width = width
16
self.__height = height
17
18
def perimeter(self):
19
"""计算周长"""
20
return (self.__width + self.__height) * 2
21
22
def area(self):
23
"""计算面积"""
24
return self.__width * self.__height
25
26
def __str__(self):
27
"""矩形对象的字符串表达式"""
28
return '矩形[%f,%f]' % (self.__width, self.__height)
29
30
def __del__(self):
31
"""析构器"""
32
print('销毁矩形对象')
33
34
35
if __name__ == '__main__':
36
rect1 = Rect()
37
print(rect1)
38
print(rect1.perimeter())
39
print(rect1.area())
40
rect2 = Rect(3.5, 4.5)
41
print(rect2)
42
print(rect2.perimeter())
43
print(rect2.area())
44
45