计算圆形的面积 编写一个父类Shape,具有一个属性color和一个方法get_area(),用于计算形状的面积。然后,基于Shape类创建子类Circle,表示圆形。子类需要实现父类的方法get_area()来计算自身的面积。 具体要求: (1)Shape类包含以下属性和方法: 属性:color(字符串类型,表示颜色) 方法:get_area()(计算并返回面积,方法体为空,由子类实现) (2)Circle类是Shape类的子类,包含以下属性和方法: 属性:radius(浮点数类型,表示圆形的半径) 方法:重写并实现get_area()方法,根据圆形的半径计算并返回面积。 请根据要求,补全代码。 import math class Shape: def ① : self.color = color def get_area(self): Pass class Circle( ② ): def ③ : super().__init__(color) self.radius = radius def get_area(self): return math.pi * self.radius ** 2 circle = ④ ("blue", 2) print( ⑤ )
正确答案:补全答案(Python圆形面积):①__init__(self,color) ②Shape ③__init__(self,color,radius) ④Circle ⑤circle.get_area()。核心:继承与多态。
试卷来源:202503 青少年软件编程等级考试Python六级真题