我无法默认参数,我不知道如何使用(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);
}
答案 0 :(得分:2)
不要重新发明轮子。使用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;
}
答案 1 :(得分:1)
你可以简单地做
public NewTry() {
this(new int[] {0,0}, new int[] {1,0}, new int[] {1,1});
}
等
也就是说,如果要在Java
中传递“常量”整数数组{0,0},只需将其作为new int[] {0,0}
传递。
答案 2 :(得分:0)
这指的是类,在本例中是指Newtry。由于这些数组是类的字段,因此必须在构造函数中引用它们:
public Newtry(){
this.p1 = {1,2,3...};
this.p2 = {3,4,5...};
this.p3 = {6,7,8...};
}