【发布时间】:2013-11-17 12:39:10
【问题描述】:
我想知道这种构造函数链调用在java中是否可行? 我正在扩展基本 JButton 类,我需要首先初始化超级变量,然后使用默认构造函数初始化我的类。
public CustomButton(){
try {
URL inp = CustomButton.class.getResource("/icons/noa_en/buttonBackground.png");
background = ImageIO.read(inp);
} catch (IOException e) {
e.printStackTrace();
}
}
public CustomButton(ImageIcon img){
super(img);
this();
}
或:
public CustomButton(ImageIcon img){
this();
super(img);
}
【问题讨论】:
-
你试过吗?结果如何?
-
是的,在这两种情况下,它都会给我“构造函数调用必须是构造函数中的第一条语句”。我正在使用 ecipe IDE。
-
是的,但仍然给我同样的错误。如果你不相信我,请先尝试。
-
@armin this 或 super 应该是第一行,不能同时调用
-
那会造成很大的问题,因为 this() 也会调用 super 方法。
标签: java constructor