设计一个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()))
ice_software涵
发布了174 篇原创文章 · 获赞 442 · 访问量 11万+
私信
关注
标签:Python,self,09,color,def,print,Circle,myCircle
来源: https://blog.csdn.net/weixin_44015669/article/details/104102145