作者king08 (糊涂蛋)
看板java
标题Re: 有关继承的问题
时间Sat May 6 19:38:38 2006
※ 引述《[email protected] (ture)》之铭言:
: ※ 引述《[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
: }
这是修饰字的问题:
像public class Super{
[public][proteted][default][private] Type MethodName (Type){}
}
如果你清楚[]内的修饰子所代表是何种意思,你就知道为什麽会编译不过了
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 61.57.129.60