下面的代码显示了一个使用double
点的矩形类,它们也存储在一个对象中。矩形和矩形中的点是不可变的,因为它们不需要更改。我想提供复制(创建新的点对象)或引用构造函数中提供的点的能力,但我能想到的唯一方法是添加一个布尔参数,指定调用者是否想要制作副本或参考。
这是为了扩展性,虽然它可能不是最重要的,但我想要这个选项。但是,我不喜欢使用boolean参数实现它的方式。有没有办法让两个构造函数采用相同的参数,一个用于引用,一个用于复制?或原型中是否有等效的C ++参数自动定义,因此不需要调用者指定?我曾经考虑过使用varargs,但是调用者可以发送无限量的参数作为垃圾,可能导致堆栈溢出,我认为......
/**
* An immutable, double-precision floating-point (64-bit doubles x4) rectangle.
* The rectangle can be made to reference existing points, or to create new points. Since the points
* are also immutable, this is acceptable, as it is guaranteed they cannot change.
* @author Bill
*/
public class DoubleRect {
public final DoublePoint topLeft;
public final DoublePoint bottomRight;
public DoubleRect(DoublePoint setTopLeft, DoublePoint setBottomRight, boolean makeCopies) {
if(makeCopies == true) {
topLeft = new DoublePoint(setTopLeft);
bottomRight = new DoublePoint(setBottomRight);
}
else {
topLeft = setTopLeft;
bottomRight = setBottomRight;
}
}
}
更新:感谢所有帮助我找出要做的事情。这就是我重新编码的方式。
/**
* An immutable, double-precision floating-point (64-bit) rectangle.
* The rectangle can be made to reference existing points, or to create new points. Since the points
* are also immutable, referencing the points is acceptable, as it is guaranteed they cannot change.
* @author Bill
*/
public class DoubleRect {
public final DoublePoint topLeft;
public final DoublePoint bottomRight;
/**
* This constructor will reference the passed objects rather than duplicating them.
* See the static factory method createWithClonedPoints() for making internal copies of the point objects
* @param setTopLeft Double point designating the top left coordinate
* @param setBottomRight Double point designating the bottom right coordinate
*/
public DoubleRect(DoublePoint setTopLeft, DoublePoint setBottomRight) {
topLeft = setTopLeft;
bottomRight = setBottomRight;
}
/**
* This constructor will create new immutable points within this object using the coordinates specified
*/
public DoubleRect(double top, double left, double right, double bottom) {
topLeft = new DoublePoint(left, top);
bottomRight = new DoublePoint(right, bottom);
}
public static DoubleRect createWithClonedPoints(DoublePoint topLeft, DoublePoint bottomRight) {
return new DoubleRect(topLeft.x, topLeft.y, bottomRight.x, bottomRight.y);
}
}
答案 0 :(得分:2)
有没有办法可以让两个构造函数采用相同的参数,一个用于引用,一个用于复制?
在这种情况下,建议使用多个静态工厂方法而不是构造函数。
答案 1 :(得分:1)
有没有办法让两个构造函数采用相同的参数......
没有。在Java中,每个构造函数必须具有不同的签名。
...或原型中是否有等效的C ++参数自动定义,因此不需要调用者指定?
没有直接的等价物。但是你可以这样:
public DoubleRect(DoublePoint topLeft, DoublePoint bottomRight,
boolean makeCopies) {
...
}
public DoubleRect(DoublePoint topLeft, DoublePoint bottomRight) {
this(topLeft, bottomRight, false);
}
在你的用例中它运行得非常好,在你有大量可选参数的用例中,这种方法变得笨拙和/或不可行。
但是,我同意工厂方法或工厂对象可以是更好的解决方案。
答案 2 :(得分:0)
为什么不使用多态策略你可以这样做:
public class DoubleRect {
public final DoublePoint topLeft;
public final DoublePoint bottomRight;
public DoubleRect() {
topLeft = new DoublePoint();
bottomRight = new DoublePoint();
}
public DoubleRect(DoublePoint setTopLeft, DoublePoint setBottomRight)
{
topLeft = setTopLeft;
bottomRight = setBottomRight;
}
}
这种类型的构造函数声明在java上有效。