You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
618 B

/**
* 三角形(已知底与高):面积 = 底 * 高 / 2
*/
public class Triangle extends Shape {
private final double base;
private final double height;
public Triangle(double base, double height) {
if (base <= 0 || height <= 0) {
throw new IllegalArgumentException("底和高必须为正数");
}
this.base = base;
this.height = height;
}
public double getBase() {
return base;
}
public double getHeight() {
return height;
}
@Override
public double getArea() {
return base * height / 2.0;
}
}