简答题 偏难
#16445

计算圆形的面积 编写一个父类Shape,具有一个属性color和一个方法get_area(),用于计算形状的面积。然后,基于Shape类创建子类Circle,表…

计算圆形的面积

编写一个父类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(        ⑤        ) 

未登录也能判分,登录后才会记录
答案与解析
正确答案:<pre class="brush:python;toolbar:false">import math class Shape:     def __init__(self,color):         self.color = color     def get_area(self):         Pass class Circle(Shape):     def __init__(self,color,radius):         super().__init__(color)         self.radius = radius     def get_area(self):         return math.pi * self.radius ** 2 circle =Circle("blue", 2) print(circle.get_area())</pre>
解析

评分标准:

(1)__init__(self,color) 或等效答案;(2分)

(2)Shape 或等效答案;(2分)

(3)__init__(self,color,radius) 或等效答案;(3分)

(4)Circle 或等效答案;(1分)

(5)circle.get_area() 或等效答案。(2分)