Re: [問題] 關於建構子與繼承
※ 引述《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 )
討論串 (同標題文章)
java 近期熱門文章
PTT數位生活區 即時熱門文章