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.
 
 
 

69 lines
1.3 KiB

package com.example.shape;
/**
* 三角形类
* 继承自Shape抽象类,实现getArea()方法
*/
public class Triangle extends Shape {
private double base;
private double height;
/**
* 构造方法
* @param base 三角形的底
* @param height 三角形的高
*/
public Triangle(double base, double height) {
this.base = base;
this.height = height;
}
/**
* 计算三角形面积
* @return 三角形的面积
*/
@Override
public double getArea() {
return base * height / 2;
}
/**
* 获取三角形的底
* @return 三角形的底
*/
public double getBase() {
return base;
}
/**
* 设置三角形的底
* @param base 三角形的底
*/
public void setBase(double base) {
this.base = base;
}
/**
* 获取三角形的高
* @return 三角形的高
*/
public double getHeight() {
return height;
}
/**
* 设置三角形的高
* @param height 三角形的高
*/
public void setHeight(double height) {
this.height = height;
}
/**
* 绘制三角形
*/
@Override
public void draw() {
System.out.println("绘制一个底为 " + base + ",高为 " + height + " 的三角形");
}
}