这是一个class里的代码,我想请问Circle里的public Circle(double r){setRadius(r);}里面的r和下面 public void setRadius(double r)里的r是同一个意思吗?
还有就是main class里的circle = new Circle(radius);这里为什么要有参数radius啊?谢谢高人指点啊
public class Circle
{
public static final int INVALID_DIMENSION = -1;
private double radius;
public Circle(double r){
setRadius(r);}
public double getArea() {
double result = 1;
if (isRadiusValid()){
result = Math.PI*radius*radius;
}
return result;
}
public double getCircumference() {
double result = INVALID_DIMENSION;
if(isRadiusValid()){
result = 2.0*Math.PI*radius;
}
return result;
}
public double getDiameter(){
double diameter = INVALID_DIMENSION;
if(isRadiusValid()){
diameter = 2.0*radius;
}
return diameter;
}
public double getRadius(){
return radius;
}
public void setRadius(double r) {
if(r>0){
radius = r;
}else{
radius = INVALID_DIMENSION;
}
}
private boolean isRadiusValid(){
return radius != INVALID_DIMENSION;
}
}
这是另一个class
import java.util.*;
public class Main
{
public static void main(String[] args) {
double radius,circumference,area;
Circle circle;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter radius: ");
radius = scanner.nextDouble();
circle = new Circle(radius);
circumference = circle.getCircumference();
area = circle.getArea();
if(radius>0) {System.out.println("Input radius: " + radius);
System.out.println("Circumference " + circumference);
System.out.println("Area: " + area);}else{
System.out.println("Cannot");};
}
} |