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  ,想问下想法是否正确????

解决方案 »

  1.   

    我认为你错了
    你把这里改一下看看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
      

  2.   

    这个时候就体现了this的作用,呵呵
      

  3.   

    this.x = x;
    this.y = y;
    this.z = z;
    this表示对当前对象的引用
      

  4.   

    2楼的,按我的程序运行的结果是0.0-0.0-0.0.
    按3楼的方法加THIS.结果就是1.0-2.0-3.0
      

  5.   

    上面的楼数弄错了
    想问下2楼的,加了THIS.是不是就把当前对象的 X 和形式参数的 X 区分开来??
      

  6.   

    哈哈哈,构造函数里的本地变量(包括参数)名与类的成员变量相同时,在函数体里,成员变量会被屏蔽掉,一切操作都只针对函数本地变量而言的,所以要加this指针啦
      

  7.   

    x = x; 
    y = y; 
    z = z;
    程序在这一步是做了什么操作??????
      

  8.   


    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不是你想的那样,看看上面的你或许就明白了!
      

  9.   

    加this,
    局部变量和成员变量重名了.
      

  10.   

    x = x; 
    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();
    }
    }