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

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

定义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;
    }

}