我正在尝试x
在我的方法中访问私有变量 ( ),distanceFromPoint
但它似乎不起作用。我怎样才能访问它?0.0
无论其他值如何,我的方法都会一直返回。
代码
public class Pointdeclare {
private static int x;
private static int y;
Pointdeclare (int x_ , int y_ ){
this.x = x_;
this.y = y_;
}
int getX(){
return x;
}
int getY(){
return y;
}
static double distanceFromZero (){
return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
}
double distanceFromPoint(Pointdeclare point){
int distX = point.getX()- this.x;
int distY = point.getY()- this.y;
return (double) Math.sqrt(Math.pow(distX, 2) + Math.pow(distY, 2));
}
}
主班
public class main {
static Pointdeclare p1 = new Pointdeclare(6, 7);
static Pointdeclare p2 = new Pointdeclare(3, 7);
public static void main (String[] args){
System.out.println(p2.distanceFromZero());
System.out.print(p1.distanceFromPoint(p2));
}
}