怎么定义平面图形父类PlaneGraphics。和定义长方形子类Rectanale和随圆形子类Eclipse
4条回答 默认 最新
关注代码如下 , 如有帮助给个采纳谢谢
import math class PlaneGraphics: def __init__(self, shape): self.shape = shape def calculate_area(self): pass def display_shape_and_area(self): print(f"Shape: {self.shape}") print(f"Area: {self.calculate_area()}") class Rectangle(PlaneGraphics): def __init__(self, shape, width, height): super().__init__(shape) self.width = width self.height = height def calculate_area(self): return self.width * self.height class Ellipse(PlaneGraphics): def __init__(self, shape, major_axis, minor_axis): super().__init__(shape) self.major_axis = major_axis self.minor_axis = minor_axis def calculate_area(self): return math.pi * self.major_axis * self.minor_axis # 举例 rectangle = Rectangle("Rectangle", 4, 6) rectangle.display_shape_and_area() ellipse = Ellipse("Ellipse", 3, 10) ellipse.display_shape_and_area()本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报