考虑下面的类定义。如何决定存根方法是静态的还是非静态的?
class Point {
private final int x;
private final int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
// Should the methods add(), subtract() and inverseOf() be non-static ...
public Point add(Point point) {
}
public Point subtract(Point point) {
}
public Point inverseOf() {
}
// Or static?
public static Point add(Point point1, Point point2) {
}
public static Point subtract(Point point1, Point point2) {
}
public static Point inverseOf(Point point) {
}
}
final
。同时,将构造函数设为private
并添加一个静态的创建方法也是一个好主意。 - Tom Hawtin - tackline