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()))

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章