编写一个类Shape,声明计算图形面积的方法。再分别定义Shape的子类Circle(圆)和Rectangle(矩形),在两个子类中按照不同图形的面积计算公式,实现Shape类中计算面积的方法。

package advance;

abstract class Shop {
    double Area;
    public abstract void getArea();
}


class Circle extends Shop {
    private double r;
    final double PI = 3.14;

    public void setR(double r) {
        this.r = r;
    }

    public double getR() {
        return r;
    }

    public Circle(double r) {
        this.r=r;
    }

    public void getArea() {
        super.Area=r*r*PI;
    }

    @Override
    public String toString() {
        return "Circle{" +
                "Area=" + Area +
                ", r=" + r +
                ", PI=" + PI +
                '}';
    }
}

class Rectangle extends Shop {
    private double x;
   private double y;

    public void setX(double x) {
        this.x = x;
    }

    public void setY(double y) {
        this.y = y;
    }

    public double getX() {
        return x;
    }

    public double getY() {
        return y;
    }

    public Rectangle(double x, double y) {
    this.y=y;
    this.x=x;
    }

    public void getArea() {
        super.Area=x*y;
    }

    @Override
    public String toString() {
        return "Rectangle{" +
                "Area=" + Area +
                ", x=" + x +
                ", y=" + y +
                '}';
    }
}

public class Homework2 {
    public static void main(String[] args) {
        Circle circle = new Circle(5);
        Rectangle rectangle = new Rectangle(6, 5);
        circle.getArea();
        rectangle.getArea();
        System.out.println(circle.toString());
        System.out.println(rectangle.toString());

    }

}
上一篇:网络学习day1-计算机网络基础


下一篇:第二天-python基础