我对以下程序的结果感到很困惑。有人可以解释一下吗?
public class Prg {
Prg () {
this(0);
System.out.println("Hi ");
}
Prg (int x) {
this(0, 0);
System.out.println("Hello");
}
Prg (int x, int y) {
System.out.println("How are you");
}
public static void main(String[] args) {
Prg ob = new Prg ();
}
}
输出:
你好吗? 你好
喜
答案 0 :(得分:1)
this(0);
是构造函数委派的示例。在这里,您从构造函数中调用一对int
s作为参数的构造函数。要利用这个习惯用法,对另一个构造函数的调用必须是调用构造函数的第一个语句。
使用逐行调试器充分解释了其余的行为。