看板java
标 题Re: [问题] 关於建构子与继承
发信站元智大学风之塔 (Tue Sep 12 00:49:40 2006)
转信站ptt!ctu-reader!ctu-gate!news.nctu!news.ncu!news.yzu!bbs.yzu
※ 引述《[email protected] (阿萨布鲁‧湖澜)》之铭言:
> ============================================================
> 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 )