有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

可以复制或引用的java构造函数

下面的代码显示了一个使用double点的矩形类,这些点也存储在一个对象中。矩形和矩形中的点是不可变的,因为它们不需要更改。我想提供复制(创建新的点对象)或引用构造函数中提供的点的功能,但我能想到的唯一方法是添加一个布尔参数,指定调用方是否希望创建副本或引用

这是为了扩展性,虽然它可能不是最重要的,但我希望这个选项。然而,我不喜欢用布尔参数实现它的方式。有没有办法让两个构造函数使用相同的参数,一个用来引用,一个用来复制?或者在原型中有一个等价于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);
    }


}

共 (3) 个答案

  1. # 1 楼答案

    Is there a way I can make two constructors taking the same parameters, one to make a reference and one to make copies?

    在这种情况下,建议使用多个静态工厂方法,而不是构造函数

  2. # 2 楼答案

    Is there a way I can make two constructors taking the same parameters ...

    不可以。在Java中,每个构造函数必须有不同的签名

    ... or is there an equivalent to C++ parameter auto definition in the prototype so it doesn't need to be specified by the caller?

    不,没有直接的等价物。然而,你可以这样做:

    public DoubleRect(DoublePoint topLeft, DoublePoint bottomRight,
             boolean makeCopies) {
        ...
    }
    
    public DoubleRect(DoublePoint topLeft, DoublePoint bottomRight) {
        this(topLeft, bottomRight, false);
    }
    

    在您的用例中,它工作得非常好,在您有许多可选参数的用例中,这种方法变得很难操作和/或不可行


    然而,我同意工厂方法或工厂对象可能是更好的解决方案

  3. # 3 楼答案

    为什么不使用多态性策略呢?你可以这样做:

    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上有效