您当前的位置: 首页 >  哆啦A梦_i Python

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

哆啦A梦_i 发布时间:2020-01-28 20:16:41 ,浏览量:6

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

关注
打赏
查看更多评论

哆啦A梦_i

暂无认证

  • 6浏览

    0关注

    566博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录