pythonbook/Python 基础教程/1.10.10 继承.py

30 lines
714 B
Python

import math
class Shape:
def __init__(self,color):
self.color = color
def area(self):
return 0
def __str__(self):
return f'color is {self.color}'
class Circle(Shape):
def __init__(self,color,r):
super().__init__(color)
self.r = r
def area(self):
return self.r**2*math.pi
class Retangle(Shape):
def __init__(self,color,a,b):
# super().__init__(color)
self.color = color
self.a = a
self.b = b
def area(self):
return self.a*self.b/2
s = Shape('red')
c1 = Circle('green',13)
r = Retangle('green',13,22)
print(s,c1,r)
print(r.area())
# 一定不要忘记在子类的init方法中调用super()._init_()