第一种:
public class point {
private double x;
private double y;
public point(){
this(0,0);
}
public point(double x, double y){
this.x = x;
this.y = y;
}
}
第二种
public class point {
private double x;
private double y;
public point(){
this.x = this.y = 0;
}
public point(double x, double y){
if(x > 0||y > 0){
this.x = x;
this.y = y;
}else{
this();
}
}
很简单,第二种写法有编译错误Constructor call must be the first statement in a constructor。
java语法归档,调用构造函数必须是第一条语句,你这样显然是不行的。
public point(double x, double y){
if(x > 0||y > 0){
this.x = x;
this.y = y;
}else{
this();
}
}
中的this(); 应该放在方法的第一句(构造方法调用构造方法)
看错误提示 构造方法调用构造方法应该在第一句