【发布时间】:2023-03-30 19:16:01
【问题描述】:
我希望在这个子类的每个重载构造函数中执行一些代码(例如,“setBackground(Color.BLACK);”)(或者我也可以表示“一些代码”必须在这个子类的实例之后执行创建)。但是这个“一些代码”只能在我们的子类中使用/调用,而且只能使用一次。并且这个子类的每一个重载的构造函数必须总是调用超类的适当的重载构造函数。
public class JLProgressBar extends JProgressBar{
public JLProgressBar(){
super();
}
public JLProgressBar(int orient){
super(orient);
}
public JLProgressBar(int min, int max){
super(min,max);
}
public JLProgressBar(int orient, int min, int max){
super(orient,min,max);
}
public JLProgressBar(BoundedRangeModel newModel){
super(newModel);
}
}
【问题讨论】:
-
initialiser block 能满足您的需求吗?
-
@BeUndead,是的,非常感谢!但不好的是,它在构造函数之前执行。
-
它们在构造函数中调用
super之后运行。因此,在您发布的上下文中,它应该有效地在所有这些之后。这些构造函数中是否还有其他未在此处发布的代码运行? -
@BeUndead,重新检查一下 - 你是对的!一切正常。谢谢!
标签: java object constructor instance