有人可以向我解释一下参考(非原始)数据类型的工作原理吗?主要是如何将数据输入到它们以及如何检查它们持有的数据? 你可以用这个代码作为例子。
public class Example{
public static void main(String [] args){
Circle c= new Circle();
System.out.println():
}
}
public class Circle{
Circle round;
public Circle(){
}
public Circle numPlacment(){
round=new Circle(2); //I would like circle to contain the value of '2'
return round;
}
public String toString(){
StringBuilder b= new StringBuilder();
b.append(round);
return String.format("%4s",b);
}
}
答案 0 :(得分:3)
你的代码有点荒谬。看看它应该如何完成可能更容易:
public class Example{
public static void main(String [] args) {
// create a new circle with radius 2
Circle c= new Circle(2);
// Print that circle
System.out.println(c);
}
}
class Circle {
// The instance variable that stores the radius for this circle
double radius;
// Create a new Circle given a radius
public Circle(double radius) {
// assign the given radius parameter to the instance variable
this.radius = radius;
}
public String toString() {
StringBuilder b= new StringBuilder();
b.append(radius);
return String.format("%4s",b);
}
}