Re: [問題] 關於建構子與繼承

看板java作者時間19年前 (2006/09/14 02:01), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串4/4 (看更多)
※ 引述《Taique.bbs@ptt.cc (阿薩布魯‧湖瀾)》之銘言: > ============================================================ > class B extends A{ > private int x,y; > B(int x, int y){ super(x, y); 因為class A已經有你寫的A(int x, int y),所以不會再幫你加上沒有參數的A() 所以如果你class B的建構子沒有自己呼叫super(x, y)的話,預設會幫你呼叫super() 此時就會找不到可以呼叫的constructor 解決方法是自己呼叫super(x, y) > this.x=x;this.y=y;} > void printXY(){System.out.print("B: x=" + x + ", y=" + y);} > public static void main(String[] args){ > A t = new B(1,2); > t.printXY();} > } > class A{ > private int x,y; > A(int x, int y){ > this.x=x;this.y=y;} > void printXY(){System.out.print("A: x=" + x + ", y=" + y);} > } > ============================================================= > 上面的程式碼為什麼會產生找不到class A的Constructor的compile error? > 不是應該會有 Default Constructor 嗎? -- ※ Origin: 元智大學 風之塔 <bbs.yzu.edu.tw> ※ From : 220-137-95-130.dynamic.hinet.net ※ X-Info: Re: [問題] 關於建構子與繼承 ※ X-Sign: 12GB4T4ex0jrhG3/0Rx6 (06/09/12 0:49:40 )
文章代碼(AID): #1524Tn00 (java)
文章代碼(AID): #1524Tn00 (java)