设计一个Circle类,包括圆心位置、半径、颜色属性。编写构造方法进行属性初始化,编写类方法计算周长与面积。方法一
class Circle: location=(0,0) r=0 color="" def __init__(self): self.location=(100,100) self.r=10 self.color="white" def GetGirth(self): PI=3.14 print("圆的周长:") print(2*PI*self.r) def GetArea(self): PI=3.14 print("圆的面积") print(PI*self.r*self.r) myCircle=Circle() myCircle.GetGirth() myCircle.GetArea()
#方法二
class Circle: def __init__(self,location,r,color): self.location =location self.r=r self.color=color def GetGirth(self): return 2*3.14*self.r def GetArea(self): return 3.14*self.r*self.r myCircle=Circle((200,200),10,"红色") print("圆的周长=%0.2f"%(myCircle.GetGirth())) print("圆的面积=%0.2f"%(myCircle.GetArea()))