我执行了以下代码,当我尝试打印它时,除了 0.0 打印之外没有任何值:
public class RegularPolygon {
private int numSides; //# sides
private double sideLength; //side length
private double inscribedRadius; //radius of inscribed circle
private void calcr(){
inscribedRadius = .5 * sideLength * 1/Math.tan(Math.PI / numSides);
}
}
public double getr(){
return inscribedRadius;
}
主要的:
RegularPolygon poly = new RegularPolygon(4, 10);
System.out.println(poly.getr());
输出:
0.0
构造函数:
public RegularPolygon(int newNumSides, double newSideLength){
numSides = newNumSides;
sideLength = newSideLength;
}
是什么阻止它打印出来?