package top.qaqaq.java.P229;
/*
* 编写两个类,TriAngle和TriAngleTest,其中TriAngle类中声明私有的底边长base和高height,同时声明公共方法访问私有变量。
* 此外,提供类必要的构造器。另一个类中使用这些公共方法,计算三角形的面积。
*
*
*
*
*/
public class TirAngle { //angle:角 angel:天使
private double base;//底边长
private double height;//高
private double area;
public TirAngle() {
}
public TirAngle(double b,double h) {
base = b;
height = h;
System.out.println(base * height / 2);
}
public void setBase(double b) {
base = b;
}
public double getBase() {
return base;
}
public void setHeight(double h) {
height = h;
}
public double getHeight() {
return height;
}
}
package top.qaqaq.java.P229;
public class TriAngleTest {
public static void main(String[] args) {
TirAngle t1 = new TirAngle();
t1.setBase(2.0);
t1.setHeight(2.4);
// t1.base = 2.5;//The field TirAngle.base is not visible
// t1.height = 4.3;
System.out.println("base:" + t1.getBase() + ",height:" + t1.getHeight());
TirAngle t2 = new TirAngle();
System.out.println("base:" + t2.getBase() + ",height:" + t2.getHeight());
}
}