weixin_69932367 2023-06-14 15:01 采纳率: 83.3%
浏览 43
已结题

python计算面积问题

怎么定义平面图形父类PlaneGraphics。和定义长方形子类Rectanale和随圆形子类Eclipse

  • 写回答

4条回答 默认 最新

  • 全栈若城 新星创作者: 编程技术技术领域 2023-06-14 15:06
    关注

    代码如下 , 如有帮助给个采纳谢谢

    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()
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 6月22日
  • 已采纳回答 6月14日
  • 创建了问题 6月14日