// MyRectangle.java 长方形
public class MyRectangle {
private double width;
private double height;
public MyRectangle(double width, double height) {
this.width = width;
this.height = height;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getArea() {
return width * height;
}
}
//MyTriangle.java 三角形
public class MyTriangle extends MyRectangle {
public MyTriangle(double width, double height) {
super(width, height);
}
@Override
public double getArea() {
return 0.5 * super.getArea();
}
}
// MyCircle.java 圆形
public class MyCircle extends MyRectangle {
public MyCircle(double radius) {
super(radius, radius);
}
@Override
public double getArea() {
return Math.PI * super.getArea();
}
}
// MyTest.java 测试
public class MyTest {
public static void main(String[] args) {
// 测试
// 长方形面积
System.out.println(new MyRectangle(1, 1).getArea());
// 圆形面积
System.out.println(new MyCircle(1).getArea());
// 三角形面积
System.out.println(new MyTriangle(1, 1).getArea());
}
}
// 结果
1.0
3.141592653589793
0.5