Topic: 接口合并时的名称冲突问题 |
Print this page |
1.接口合并时的名称冲突问题 | Copy to clipboard |
Posted by: hcs_1018 Posted on: 2007-10-26 23:01 最近在学习Think in JAVA一书时,在看到"接口合并时的名称冲突问题"时不甚理解透彻. // c08 InterfaceCollision.java interface I1 {void f();} interface I2 {int f(int i);} interface I3 {int f();} class C { public int f(){return 1;} }; class C2 implements I1,I2 { public void f() {} public int f(int i) {return 1;} }; class C3 extends C implements I2 { public int f(int i) {return 1;} }; class C4 extends C implements I3 { public int f() {return 1;} }; class C5 extends C implements I1 {} // C5 interface I4 extends I1,I3 {} // I4 public class InterfaceCollision { public static void main(String[] args){ } }; 当加上 C5 ,I4时候就会报如下错误. E:\JAVA>InterfaceCollision.java:29: f() in C cannot implement f() in I1; attempt ing to use incompatible return type found : int required: void class C5 extends C implements I1 ^ InterfaceCollision.java:33: types I3 and I1 are incompatible; both define f(), but with different return type interface I4 extends I1,I3 {} ^ 2 errors 但书上给出的解释理解不了.不知有那位大哥提点下.. |
2.Re:接口合并时的名称冲突问题 [Re: hcs_1018] | Copy to clipboard |
Posted by: JiafanZhou Posted on: 2007-10-27 20:57 Basically you have defined 3 interfaces and 3 methods respectively. Two of the three methods contain the same method name/parameters (which is nothing) and different return type. If there are any concrete classes implement both these two interfaces, let me ask you a question... How could the derived class differentiate which method to overwrite? Maybe you will think about provide both of these two methods. but it is against the method overloading Java language specification, is it? Think about that a little while, and you will get the answer, Regards, Jiafan |
3.Re:接口合并时的名称冲突问题 [Re: HenryShanley] | Copy to clipboard |
Posted by: hcs_1018 Posted on: 2007-10-28 23:11 HenryShanley wrote: 谢谢 HenryShanley大哥的 指导! 我是这样理解的.对于derived class的C5 完全继承了class C的函式int f() 并且又要实现interface I1的定义.既然return的类型不同.那就无法 overwrite了.也就无法区分勒. 那样编译器报告说 在C 中f()不能在I1接口中实现不同的返回类型的f()...不知我这样理解是否恰当...请指教下..谢谢 |
4.Re:接口合并时的名称冲突问题 [Re: hcs_1018] | Copy to clipboard |
Posted by: JiafanZhou Posted on: 2007-10-30 20:24 Yes, it is against the Java syntax rule. |
5.Re:接口合并时的名称冲突问题 [Re: hcs_1018] | Copy to clipboard |
Posted by: chenshuai123 Posted on: 2007-11-05 00:33 问个问题,HenryShanley大哥,是不是接口不能继承或者实现 类? |
6.Re:接口合并时的名称冲突问题 [Re: chenshuai123] | Copy to clipboard |
Posted by: JiafanZhou Posted on: 2007-11-07 16:55 chenshuai123 wrote: No, never. Class implements interface, not the other way around. |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |