看板java
標 題Re: 有關繼承的問題
發信站吟風‧眺月‧擎天崗 (Sat May 6 18:03:24 2006)
轉信站ptt!ctu-reader!news.nctu!BBS of NCUE BioDept
※ 引述《[email protected] (資管四霸征服天下)》之銘言:
> ※ 引述《[email protected] (雙面虎)》之銘言:
> 程式碼如下 請各位高手幫幫忙
> class Parent {
> int i = 30;
> int sum(int j) {
> return i+j;
> }
> int sum(){ return 333; } // 我不去掉
> }
> class Child extends Parent {
> int i = 10;
> ////////////////////////////////////////////////////
> // int sum() { // 但是去掉這段,就可以編譯了 //
> // return this.i + super.i; //
> // } //
> ////////////////////////////////////////////////////
> }
public class ex {
public static void main(String[] argv) {
Child c = new Child();
test(c);
}
static void test(Parent p) {
^^^^^^
因為你已經cast成 Parent 接著呢 你的Parent class中並沒有 sum()
那麼 compile 就不會過囉....
我猜的....
System.out.println("p.sum():" + p.sum() + " p.sum(20):" + p.sum(20));
} //變成333 ^^^^^^^^50
}
--
→↓ O
rigin:
彰化師大生物系˙吟風‧眺月‧擎天崗 micro.bio.ncue.edu.tw
↑← Au
thor:
CubicU 從
59-114-135-247.dynamic.hinet.net 發表