class Point {
double x, y, z; //double类型的成员变量被自动赋值0.0 0.0 0.0
Point(double x, double y, double z) { //在这步,X Y Z 被赋值1.0 2.0 3.0
x = x;
y = y;
z = z; //这步,X Y Z被赋值0.0 0.0 0.0
}
void weiZhi() {
System.out.println(x + "-" + y + "-" + z);
}
}
public class TestPoint {
public static void main(String[] args) {
Point p = new Point(1.0, 2.0, 3.0);
p.weiZhi();
}
}因此,程序运行输出的是0.0-0.0-0.0 ,想问下想法是否正确????
double x, y, z; //double类型的成员变量被自动赋值0.0 0.0 0.0
Point(double x, double y, double z) { //在这步,X Y Z 被赋值1.0 2.0 3.0
x = x;
y = y;
z = z; //这步,X Y Z被赋值0.0 0.0 0.0
}
void weiZhi() {
System.out.println(x + "-" + y + "-" + z);
}
}
public class TestPoint {
public static void main(String[] args) {
Point p = new Point(1.0, 2.0, 3.0);
p.weiZhi();
}
}因此,程序运行输出的是0.0-0.0-0.0 ,想问下想法是否正确????
你把这里改一下看看this.x = x;
this.y = y;
this.z = z; Point(double x, double y, double z) { //在这步,X Y Z 被赋值1.0 2.0 3.0
x = x;
y = y;
z = z; //这步,X Y Z被赋值0.0 0.0 0.0
}
你这里不是给double x, y, z赋值,所以double x, y, z还是0.0 0.0 0.0而你System.out.println(x + "-" + y + "-" + z)要打印的是double x, y, z
this.y = y;
this.z = z;
this表示对当前对象的引用
按3楼的方法加THIS.结果就是1.0-2.0-3.0
想问下2楼的,加了THIS.是不是就把当前对象的 X 和形式参数的 X 区分开来??
y = y;
z = z;
程序在这一步是做了什么操作??????
class Point {
public static double x, y, z; //double类型的成员变量被自动赋值0.0 0.0 0.0 Point(double x, double y, double z) { //在这步,X Y Z 被赋值1.0 2.0 3.0
this.x = x;
this.y = y;
z = z; //这步,X Y Z被赋值0.0 0.0 0.0
}
void weiZhi() {
System.out.println(this.x + "-" + this.y + "-" +this.z);
}
}
public class TestPoint {
public static void main(String[] args) {
System.out.println(Point.x + "-" + Point.y + "-" + Point.z);
Point p = new Point(1.0, 2.0, 3.0);
p.weiZhi();
System.out.println(Point.x + "-" + Point.y + "-" + Point.z);
}
}
LZ不是你想的那样,看看上面的你或许就明白了!
局部变量和成员变量重名了.
y = y;
z = z; //这步,X Y Z被赋值0.0 0.0 0.0
不是被赋值为0.0 ,相当于什么操作也没做class Point {
public static double x, y, z; // double类型的成员变量被自动赋值0.0 0.0 0.0 Point(double x, double y, double z) { // 在这步,X Y Z 被赋值1.0 2.0 3.0
x = 4; //局部变量赋值
y = y; //无效的赋值 The assignment to variable y has no effect
Point.z = z; // 这步,X Y Z被赋值0.0 0.0 0.0
System.out.println(x + "-" + y + "-" + z);
System.out.println(this.x + "-" + this.y + "-" + this.z);
} void weiZhi() {
System.out.println(this.x + "-" + this.y + "-" + this.z);
}
}public class Test {
public static void main(String[] args) {
Point p = new Point(1.0, 2.0, 3.0);
p.weiZhi();
}
}