3 回答

TA贡献1856条经验 获得超5个赞
public class Foo extends Baz { private final Bar myBar; public Foo(String arg1, String arg2) { // ... // ... Some other stuff needed to construct a 'Bar'... // ... final Bar b = new Bar(arg1, arg2); super(b.baz()): myBar = b; }}
public class Foo extends Baz { private final Bar myBar; private static Bar makeBar(String arg1, String arg2) { // My more complicated setup routine to actually make 'Bar' goes here... return new Bar(arg1, arg2); } public Foo(String arg1, String arg2) { this(makeBar(arg1, arg2)); } private Foo(Bar bar) { super(bar.baz()); myBar = bar; }}

TA贡献1880条经验 获得超4个赞
this(fn())
super(this.x = 5);super(this.fn());super(fn());super(x);super(this instanceof SubClass);// this.getClass() would be /really/ useful sometimes.
class MyBase { MyBase() { fn(); } abstract void fn();}class MyDerived extends MyBase { void fn() { // ??? }}
MyDerived.fn
MyDerived
ThreadLocal
. ;(
this
NullPointerException
2018年3月编辑:this
在历史上,这个()或超级()必须是构造函数中的第一个。这种限制从来不受欢迎,被认为是武断的。有一些微妙的原因,包括特别是对请求的核查,促成了这一限制。多年来,我们已经在VM级别解决了这些问题,以至于考虑取消这个限制变得非常实际,不仅对记录,而且对所有构造函数都是如此。
添加回答
举报