在java中的超级调用之前创建一个对象

考虑到那些不起作用的简单java代码:

public class Bar extends AbstractBar{ private final Foo foo = new Foo(bar); public Bar(){ super(foo); } } 

我需要在super()调用之前创建一个对象,因为我需要在母类中推送它。

我不想使用初始化块,我不想做类似的事情:

super(new Foo(bar))在我的构造函数中..

如何在超级电话之前将数据发送到母class?

如果必须将Foo存储在字段中,则可以执行以下操作:

 public class Bar extends AbstractBar{ private final Foo foo; private Bar(Foo foo) { super(foo); this.foo = foo; } public Bar(){ this(new Foo(bar)); } } 

否则super(new Foo(bar))对我来说看起来很合法,如果你愿意,可以将new Foo(bar)包装成static方法。

另请注意,字段初始值设定项(如示例中所示)和初始化程序块也无济于事,因为它们在超类构造函数之后运行。 如果field被声明为final你的例子将无法编译,否则你将在超类构造函数中得到null

这在java中是不可能的。 唯一可能的解决方案是超级构造函数中的新调用。

如果foo对象可以在实例之间共享,则可以将其声明为静态

 public class Bar extends AbstractBar{ private static final Foo foo = new Foo(bar); public Bar(){ super(foo); } } 

如果超类在你的控制之下,你可以重构它并使用模板方法模式将对象拉入构造函数而不是从子类中引用它。 这适用于hollywod原则:不要打电话给我们,我们会打电话给你;)

 public abstract class AbstractBar{ private Object thing; public AbstractBar(){ this.thing = this.createThatThing(); } protected abstract Object createThatThing(); } public class Bar extends AbstractBar { // no constructor needed protected Object createThatThing(){ return new Thing(); } } 
 class AbstractBar{ public AbstractBar() { } public AbstractBar(Foo t) { } } class Bar extends AbstractBar{ static Foo t=null; public Bar() { super(t=new Foo()); } } class Foo{...}