下面的代码将查找两条线的交点并返回点对象。如果点只会被IntersectionOf2Lines类创建,那么我应该将点设为嵌套类吗?如果不是,为什么不是呢?谢谢。
class Point {
private final int x;
private final int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
int getX() {
return x;
}
int getY() {
return y;
}
}
public class IntersectionOf2Lines {
public static Point calculateIntersection(Line line1, Line line2) {
int x = (line2.getConstant() - line1.getConstant()) / (line1.getSlope() - line2.getSlope());
int y = line1.getSlope() * x + line1.getConstant();
return new Point(x, y);
}
calculateIntersection
方法放在一个单独的类中,而不是Point类中。 - Anti Earth