一个简单的JAVA程序,高手帮我看看!
发布网友
发布时间:2023-10-17 14:51
我来回答
共1个回答
热心网友
时间:2024-08-23 08:42
class Point{
double x,y,z;
Point(double x,double y ,double z){
this.x=x;
this.y=y;
this.z=z;
}
void setx(double x){
this.x=x;
}
void sety(double y){
this.y=y;
}
void setz(double z){
this.z=z;
}
double getDistance(Point p1){
System.out.println(" "+x+" "+y+" "+z);
return (x-p1.x)*(x-p1.x)+(y-p1.y)*(y-p1.y)+(z-p1.z)*(z-p1.z);
}
}
public class TestApp{
public static void main (String [] args){
Point p1 = new Point(1.0,2.0,3.0);
Point p2 = new Point(0.0,0.0,0.0);
System.out.println(p1.getDistance(p2));
p1.setx(5.0);
p1.sety(4.0);
p1.setz(3.0);
System.out.println(p1.getDistance(new Point(1.0,1.0,1.0)));
}
}
没有指定X,所以导致默认取值为0.
需要用this来指定你要赋值的是对象里面的变量。