Re: 有關繼承的問題

看板java作者 (糊塗蛋)時間19年前 (2006/05/06 19:29), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串3/5 (看更多)
※ 引述《CubicU.bbs@micro.bio.ncue.edu.tw (ture)》之銘言: : ※ 引述《deepdish.bbs@binary.csie.ncu.edu.tw (資管四霸征服天下)》之銘言: : > 程式碼如下 請各位高手幫幫忙 : > 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 : } 這是修飾字的問題: 像public class Super{ [public][proteted][default][private] Type MethodName (Type){} } 如果你清楚[]內的修飾子所代表是何種意思,你就知道為什麼會編譯不過了 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.57.129.60
文章代碼(AID): #14N8YKCi (java)
文章代碼(AID): #14N8YKCi (java)