自定义类如下:
import math class Circle: # Construct a circle object def __init__(self, radius = 1): self.radius = radius def getPerimeter(self): return 2 * self.radius * math.pi def getArea(self): return self.radius * self.radius * math.pi def setRadius(self, radius): self.radius = radius
测试类如下:
import sys sys.path.append(r'/home/MyTrain/Python/Class') from Circle import Circle c = Circle(5) print("Redius: ", c.radius) print("Perimeter: ", c.getPerimeter()) print("Area: ", c.getArea())
>>python test_circle.py
运行结果:
('Redius: ', 5)
('Perimeter: ', 31.415926535897931)
('Area: ', 78.539816339744831)