3 回答

TA貢獻1827條經(jīng)驗 獲得超8個贊
FileInputStream stream = new FileInputStream();

TA貢獻1789條經(jīng)驗 獲得超8個贊
public class Son extends Super{ // If you dont declare a constructor of any type, adefault one will appear. public Son(){ // If you dont call any other constructor in the first line a call to super() will be placed instead. super(); }}

TA貢獻1817條經(jīng)驗 獲得超14個贊
class Super { protected final Number value; public Super(Number value){ this.value = value; }}class Sub { public Sub(){ super(Integer.valueOf(0)); } void doSomeStuff(){ // We know this.value is an Integer, so it's safe to cast. doSomethingWithAnInteger((Integer)this.value); }}// Client code:Sub s = new Sub(Long.valueOf(666L)): // Devilish invocation of Super constructor!s.doSomeStuff(); // throws ClassCastException
class Super { private final String msg; Super(String msg){ if (msg == null) throw new NullPointerException(); this.msg = msg; }}class Sub { private final String detail; Sub(String msg, String detail){ super(msg); if (detail == null) throw new NullPointerException(); this.detail = detail; } void print(){ // detail is never null, so this method won't fail System.out.println(detail.concat(": ").concat(msg)); }}// Client code:Sub s = new Sub("message"); // Calling Super constructor - detail is never initialized!s.print(); // throws NullPointerException
添加回答
舉報