并感谢每个人修复格式等,这里全新
我最近开始学习java,在一次练习中发生了一个问题,抱歉,如果我错过了发布规则:
从一个MyPoint到另一个Mypoint计算距离,我决定使用用于MyPoint吸气另一个因为x和y为另一个应该是私有的,并且不能在点操作(another.x another.y)中使用;
public class MyPoint {
private int x;
private int y;
public int getX() {
return x;
}
public int getY() {
return y;
}
public double distance(MyPoint another) {
int xDiff = this.x - another.getX(); //getter
int yDiff = this.y - another.getY(); // getter
return Math.sqrt(xDiff * xDiff + yDiff * yDiff);
}
}
public class TestMyPoint {
public static void main(String[] args) {
MyPoint a = new MyPoint(3,0);
MyPoint b = …Run Code Online (Sandbox Code Playgroud)