Re: 有關繼承的問題
避免 pcman 使用者誤觸陣亡. 原文砍除.
※ 引述《redbug.bbs@bbs.ice.cycu.edu.tw (雙面虎)》之銘言:
程式碼如下 麻煩各位高手們幫忙
class Parent {
int i = 30;
int sum(int j) {
return i+j;
}
//////////////////////////////////////////////////////////////////////////////
// int sum()//為何去掉這段程式碼編譯就不過 錯誤訊息如下 //
// {return 333;}//ex.java:25: sum(int) in Parent cannot be applied to () //
// System.out.println("p.sum():" + p.sum() + " p.sum(20):" + p.sum(20));//
// //
//////////////////////////////////////////////////////////////////////////////
}
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) {
System.out.println("p.sum():" + p.sum() + " p.sum(20):" + p.sum(20));
} //^^^^^^40 ^^^^^^^^50
}
--
╭┬╮
│┴│╭─﹏﹏────────────── Web bbs http://bbs.ice.cycu.edu.tw
└┬┘│中原資工神秘之旅 Post by redbug │
╰﹏╯ From:220-139-90-172.dynamic.hinet. ╰﹏﹏bbs.ice.cycu.edu.tw﹏﹏Ω
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 163.25.148.49
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 4 之 5 篇):
java 近期熱門文章
PTT數位生活區 即時熱門文章