欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > python >内容正文

python

python输入半径计算公式_Python:09设计Circle类包括圆心半径、颜色属性,编写类方法计算周长与面积(2种方法)...

发布时间:2024/10/14 python 50 豆豆
生活随笔 收集整理的这篇文章主要介绍了 python输入半径计算公式_Python:09设计Circle类包括圆心半径、颜色属性,编写类方法计算周长与面积(2种方法)... 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

设计一个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

与50位技术专家面对面20年技术见证,附赠技术全景图

总结

以上是生活随笔为你收集整理的python输入半径计算公式_Python:09设计Circle类包括圆心半径、颜色属性,编写类方法计算周长与面积(2种方法)...的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。