3 回答

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超5個(gè)贊
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貢獻(xiàn)1880條經(jīng)驗(yàn) 獲得超4個(gè)贊
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
在歷史上,這個(gè)()或超級()必須是構(gòu)造函數(shù)中的第一個(gè)。這種限制從來不受歡迎,被認(rèn)為是武斷的。有一些微妙的原因,包括特別是對請求的核查,促成了這一限制。多年來,我們已經(jīng)在VM級別解決了這些問題,以至于考慮取消這個(gè)限制變得非常實(shí)際,不僅對記錄,而且對所有構(gòu)造函數(shù)都是如此。
添加回答
舉報(bào)