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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器