python物件導向練習題01

專注的阿熊發表於2021-08-02

class Shape:

     def area(self):

         pass

     def perimeter(self):

         pass

class Rectangle(Shape):

     width=0

     __high=0

     def __init__(self,width,high):

         self.width=width

         self.__high=high

     def set_high(self,high):

         self.__high=high

     def area(self):

         return self.width * self.__high

     def perimeter(self):

         return (self.width + self.__high) * 2

class Square(Rectangle):

     length=0

     def __init__(self,length):

         self.length=length

         self.set_high(length)

     def area(self):

         return self.length**2

     def perimeter(self):

         return  self.length*4

class Circle(Shape):

     r=0

     def __init__(self,r):

         self.r=r

     def area(self):

         return self.r**2*3.14

     def perimeter(self):

         return 2*3.14*self.r

class Annulus(Circle):

     inner_r=0

     def __init__(self,r,inner_r):

         self.r=r

         self.inner_r=inner_r

     def area(self):

         return super().area()-self.inner_r*3.14

     def perimeter(self):

         return super().perimeter()+self.inner_r*3.14

rect=Rectangle(20,20)

print(rect.area())

rect.set_high(10)

print(rect.area())

squre=Square(30)

print(squre.area())

circle=Circle(2)

print(circle.area())

annulus=Annulus(20,10)

print(annulus.area())

print(annulus.perimeter())

class A:

     def area(self):

         return ' 你調錯了 '

def computer_area(shape):

     if isinstance(shape,Shape):

         print(shape.area())

     else:

         print('外匯跟單gendan5.com 不屬於二維圖形 ')

     print(shape.area())

computer_area(squre)

computer_area(annulus)

computer_area(circle)

a=A()

computer_area(a)

print(isinstance(squre,Rectangle))# 判斷正方形是否是長方形的例項


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2784791/,如需轉載,請註明出處,否則將追究法律責任。

相關文章