Java中System.out.println();的输出机制和方向

来源:百度知道 编辑:UC知道 时间:2024/06/21 01:57:51
我写了一个程序,可是运行出来的结果有点让我不通。
class Point {
double x;
double y;
Point(double x, double y) {
this.x = x;
this.y = y;
}
Point(){;}

void setX(double x) {
this.x = x;
}
void setY(double y) {
this.y = y;
}

double getX() {
return x;
}
double getY() {
return y;
}
}

class Circle {
private Point o;
private double radius;
public Circle(Point p, double radius) {
o = p;
this.radius = radius;
}
public Circle(double radius) {
o = new Point(0.0, 0.0);
this.radius = radius;
}
Circle(){;}

void setPoint(double x, double y) {
o.setX(x);
o.setY(y);
}
void setRadius(double radius) {
this.radius = radius;
}

public Point getPoint() {
return o;
}
public double getRadius() {
System.out.println(Math.PI);<

System.out.println("c1: "+"("+c1.getPoint().getX()+","+c1.getPoint().getY()+") "+c1.getRadius()+" "+c1.area());
这是一句System.out.println(String str);语句,其中的str是什么呢?
str=str1+str2+str3... 那么它要输出的是str,就要先计算出str1,str2,str3...等等,所以你的语句中,它是先构建出几个子字符串,其中当然也包括"c1:","(","c1.getPoint().getX()","c1.getRadius()",等等

c1.getRadius()先输出了:3.141592653589793

c1.area() 输出了:3.141592653589793aaaaa

最后把子串连接成要输出的串:c1: (1.0,2.0) 2.0 12.566370614359172

明白怎么回事了吗?

好像和println没有关系的,应该与类的初始化有关,好像是类初始化的时候,调用了其中的方法,所以,先调用了getridius,有调用了,area,最后才是main里的输出