linkergr 2021-07-13 15:25 采纳率: 50%
浏览 29
已采纳

这个怎么做啊求解决问题啊

定义一个形状类,有一个获取面积的方法,派生出两个子类,矩形类和椭圆类,分别重写获取面积的方法,矩形和椭圆派生出两个子类,正方形和圆形再获取自己的面积,写一个测试类分别测试对应的面积

  • 写回答

1条回答 默认 最新

  • 关注

    定义shape类,矩形类和椭圆类继承shape,然后重写获取面积方法。参考:

    
    public class 继承测试 {
        /*
         * 在该类中定义两个方法,一个是 getName,用于使用反射机制获得类名称;另一个是抽象方法 getArea ,用来计算图形的面积。 (2)创建圆形类
         * Circle ,继承自 Shape ,并实现抽象方法getArea。 在 Circle
         * 类的构造方法中获得了圆形的半径,用于在getArea计算圆形的面积。 (3)创建矩形类 Rectangle ,继承自 Shape , 并实现抽象方法
         * getArea 。在 Rectangle 类的构造方法中获得了矩形的长和宽,用于在 getArea计算矩形的面积。
         * 
         */
        public static void main(String[] args) {
            Shape circle = new Circle(10);
            System.out.println("类名称是:" + circle.getName());
            float s = circle.getArea();
            System.out.println("圆的面积=" + circle.getArea());
            Shape rect = new Rectangle(10, 20);
            System.out.println("类名称是:" + rect.getName());
            System.out.println("矩形的面积=" + rect.getArea());
            s += rect.getArea();
            Shape tri = new Triangle(3, 4, 5);
            System.out.println("类名称是:" + tri.getName());
            System.out.println("三角形的面积=" + tri.getArea());
            s += tri.getArea();
            
            System.out.println("总面积:"+s);
            
    
        }
    }
    
    abstract class Shape {
        String getName() {
            return this.getClass().getName();
        }
    
        abstract float getArea();
    }
    
    class Circle extends Shape {
    
        float r;
    
        public Circle() {
        };
    
        public Circle(float r) {
            this.r = r;
        }
    
        @Override
        float getArea() {
    
            return 3.14f * r * r;
        }
    
    }
    
    class Rectangle extends Shape {
    
        float width;
        float height;
    
        public Rectangle() {
        }
    
        public Rectangle(float width, float height) {
            this.width = width;
            this.height = height;
        }
    
        @Override
        float getArea() {
    
            return width * height;
        }
    
    }
    
    class Triangle extends Shape // 三角形类
    {
        public Triangle() {
        }
    
        private float sideA, sideB, sideC;
        private boolean boo;
    
        public Triangle(float a, float b, float c) {
            sideA = a;
            sideB = b;
            sideC = c;
            if (a + b > c && a + c > b && b + c > a) {
                System.out.println("我是一个三角形");
                boo = true;
            } else {
                System.out.println("我不是一个三角形");
                boo = false;
            }
        }
    
        public float getArea() {
            float area;
            float p = (sideA + sideB + sideC) / 2.0f;
            area = (float)Math.sqrt(p * (p - sideA) * (p - sideB) * (p - sideC));
            System.out.println("三角形面积是:" + area);
            return area;
        }
    
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已采纳回答 7月13日
  • 创建了问题 7月13日

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值