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.
35 lines
789 B
35 lines
789 B
// 三角形类,继承抽象类Shape
|
|
public class Triangle extends Shape {
|
|
// 底、高
|
|
private double base;
|
|
private double height;
|
|
|
|
// 构造方法:初始化底和高
|
|
public Triangle(double base, double height) {
|
|
this.base = base;
|
|
this.height = height;
|
|
}
|
|
|
|
// 重写getArea方法:三角形面积 = 底 * 高 / 2
|
|
@Override
|
|
public double getArea() {
|
|
return base * height / 2;
|
|
}
|
|
|
|
// 可选:getter/setter方法
|
|
public double getBase() {
|
|
return base;
|
|
}
|
|
|
|
public void setBase(double base) {
|
|
this.base = base;
|
|
}
|
|
|
|
public double getHeight() {
|
|
return height;
|
|
}
|
|
|
|
public void setHeight(double height) {
|
|
this.height = height;
|
|
}
|
|
}
|