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.

43 lines
986 B

/**
* 三角形类,继承Shape,实现面积计算
*/
public class Triangle extends Shape {
// 底
private double base;
// 高
private double height;
/**
* 无参构造
*/
public Triangle() {}
/**
* 带参构造,初始化底和高
* @param base 底
* @param height 高
*/
public Triangle(double base, double height) {
this.base = base;
this.height = height;
}
/**
* 实现父类抽象方法,计算三角形面积:底×高/2
* @return 三角形的面积
*/
@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;
}
}