gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 23:13:02 26 4
gpt4 key购买 nike

我无法默认参数,我不知道如何使用(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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com