class Point {
duoble x,y,z;
  
  Point(double _x,double _y,double _z) {
x = _x;
y = _y;
z = _z;

void pointX(double _x) {
x = _x;
}
  void pointY(double _y) {
y = _y;
}
void pointZ(double _z) {
z = _z;
}
double distance(double _x,double _y,double _z) {
return ((_x - Point.x)*(_x - Point.x) + (_y - Point.y)*(_y - Point.y) + (_z - Point.z)*(_z - Point.z));
}

}public class TestPoint {
public static void main(String []args) {
Point i = new Point(1.0,2.0,3.0);
System.out.println(i.distance(3.0,3.0,3.0));
}  
 
}
算距离的平方,但是为什么会报错这个错误类?到底是怎么回事,求教大神!!

解决方案 »

  1.   

    类直接调用属性(Point.x) 这样的属性x就要是static的 
      

  2.   

    1.上面的double写成duoble了
    2.类名.属性要求属性是static的
    像你这个应该用this.x、this.y、this.z
      

  3.   

    你构建get set 方法 然后直接使用属性
    class Point {
        Double x, y, z;    public Double getX() {
            return x;
        }    public void setX(Double x) {
            this.x = x;
        }    public Double getY() {
            return y;
        }    public void setY(Double y) {
            this.y = y;
        }    public Double getZ() {
            return z;
        }    public void setZ(Double z) {
            this.z = z;
        }    Point(double _x, double _y, double _z) {
            x = _x;
            y = _y;
            z = _z;
        }    void pointX(double _x) {
            x = _x;
        }    void pointY(double _y) {
            y = _y;
        }    void pointZ(double _z) {
            z = _z;
        }    double distance(double _x, double _y, double _z) {
            return ((_x - x) * (_x - x) + (_y - y) * (_y - y) + (_z - z) * (_z - z));
        }}class TestPoint {
        public static void main(String[] args) {
            Point i = new Point(1.0, 2.0, 3.0);
            System.out.println(i.distance(3.0, 3.0, 3.0));
        }}
      

  4.   

    但是为什么我这样写又可以了类?
    class Point {
    double x,y,z;
      
      Point(double _x,double _y,double _z) {
    x = _x;
    y = _y;
    z = _z;

    void pointX(double _x) {
    x = _x;
    }
      void pointY(double _y) {
    y = _y;
    }
    void pointZ(double _z) {
    z = _z;
    }
    double distance(Point p) {
    return ((x - p.x)*(x - p.x) + (y - p.y)*(y - p.y) + (z - p.z)*(z - p.z));
    }
    }public class TestPoint {
    public static void main(String []args) {
    Point i = new Point(1.0,2.0,3.0);
    Point p = new Point(3.0,3.0,3.0);
    System.out.println(i.distance(p));
    }  
     
    }
      

  5.   

    你跟他一样的,你这里写x,实际上就是this.x。最好还是写上
      

  6.   

    我忽然感觉自己想通了,不知道这样理解对不对。最后那句输出的语句里面要求
    的是输出对象i执行方法“distance”的值,但是我在方法“distance”里面根本
    就没有定义变量x,y,z,所以报错。后面的在方法“distance”里面有使用参数
    “Point p”,而那个参数有定义变量x,y,z,所以不会报错,不知道这样理解对不对?
      

  7.   

    不对啊
    distance方法里面没有定义,但是类里面定义了啊,类的成员函数可以访问自己的成员变量的。
    你第一个之所以报错,是因为你用Point.x。只有static的属性才可以直接用类.属性
    成员变量的访问应该用(类的实例化对象).属性。比如这个p就是Point的一个对象。
    在类内部,使用成员变量时可以用this.属性,比如this.x。
    当然在不引起冲突的情况下,也可以直接写x。
    public void setX(Double x1) {
            x = x1;//等同于this.x=x1;
        }
    但是
    public void setX(Double x) {
            this.x = x;
        }
    这里的this.就不能省略。
      

  8.   

     Point.x   不能直接调用类的非静态变量。
      

  9.   

    谢谢各位,理解了,错在Point.x 不能直接调用类的非静态变量。把
    return ((_x - Point.x)*(_x - Point.x) + (_y - Point.y)*(_y - Point.y) + (_z - Point.z)*(_z - Point.z)); 改为
    return ((_x - this.x)*(_x - this.x) + (_y - this.y)*(_y - this.y) + (_z - this.z)*(_z - this.z));
    后就可以了。
      

  10.   

    说白了就是一个类与对象的关系你没弄明白,你用Point.x调用的是类变量,就要求把x定义为static,而你把Point的对象p做参数传到方法,用p.x调用的是对象的属性,面向对象有时候还是比较抽象的