java - 无法在溢出的构造函数中默认参数

标签 java

我无法默认参数,我不知道如何使用(this)语句设置数组。 我知道这听起来很愚蠢,但我从来没有用言语表达过。

package try_Constructor;

public class NewTry {

    private int[] point1;
    private int[] point2;
    private int[] point3;

    public NewTry(){

            this(0,0, 1,0, 1,1);
    }

    public NewTry(int[] point1){

        this(point1, 1,0, 1,1);
    }

    public NewTry(int[] point1, int[] point2){

        this(point1, point2, 1,1);
    }

        public NewTry(int[] point1,int[] point2,int[] point3){

            setPointsOfTry(point1, point2, point3);
        }

最佳答案

不要重新发明轮子。使用Point class 。另外,始终链接到下一个最具体的构造函数,而不是破坏链并跳到末尾。

然后...

private static final Point DEFAULT_POINT_1 = new Point(0, 0);
private static final Point DEFAULT_POINT_2 = new Point(1, 0);
private static final Point DEFAULT_POINT_3 = new Point(1, 1);

public NewTry() {
    this(DEFAULT_POINT_1);
}

public NewTry(Point point1) {
    this(point1, DEFAULT_POINT_2);
}

public NewTry(Point point1, Point point2) {
    this(point1, point2, DEFAULT_POINT_3);
}

public NewTry(Point point1, Point point2, Point point3) {
    this.point1 = point1;
    this.point2 = point2;
    this.point3 = point3;
}

关于java - 无法在溢出的构造函数中默认参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21564692/

相关文章:

Java词搜索算法

java - 如何将依赖项的测试 jar 包含到 Maven 项目的部署中?

java - 将部分代码更改为函数式范例

java - 如何为莫尔斯电码二叉树编写 Junit 测试

java - 需要访问源的 Gradle 自定义插件

从服务器向客户端发送 HashMap 时发生 Java 空指针异常

java - SWT 用户控件设计决策

java - org/eclipse/jdt/internal/compiler 包的用途是什么?

java - Spring Boot IllegalStateException ArchiveDescriptor 重用; URL 可以多次处理吗?

java - 数据扇出 Java 库