kerry_honghao 2008-11-26 10:55
浏览 177
已采纳

关于继承与泛型,谁给解释一下?

假设如下代码:

public interface I<x> {
      public void query(Map <string, object=""> map, int count);
}
public class C implements I {
      public void query(Map <string, object=""> map, int count) {}
} 

 以上代码在JDeveloper中能编译通过并运行,但是在Eclipse、NetBeans中不能编译,在Eclipse,NetBeans中需要写成:

public class C implements I {
      public void query(Map map, int count) {}
} 

 或者是

public class C implements I<c> {
      public void query(Map <string, object=""> map, int count) {}
} 

 谁能帮我解释一下,哪个是对的?


问题补充
xixix2004 写道
先看看你后面所说的工具中的JRE版本




jre均为1.5.0_06
  • 写回答

6条回答 默认 最新

  • aaron12 2008-11-26 10:55
    关注

    [quote="lifethinker"]关键是两者的编译器实现不同。上面的程序可以直接在命令行用javac编译通过,所以我估计jdeveloper使用的编译器就是javac。我以前也一直认为eclipse也是直接使用javac编译器,刚才查看了eclipse插件目录下org.eclipse.jdt.core.jar,里面包括词法分析器,语法解析器,编译器,我才确定eclipse使用了自己的编译器。至于netbeans没有研究,应该也是使用了自己的编译器。

    p.s. 在Eclipse下如果接口I中不带任何方法或者方法签名中不使用泛型,则不会出错误。

    to feng_gladys:从eclipse提示的错误:
    Name clash: The method query(Map, int) of type C has the same erasure as query(Map, int) of type I but does not override it

    这样解释也合乎情理,但是从语言规范上来说应该是容许这种情况发生的,不应该有任何错误,至多也只是警告,不过提示错误也未尝不是坏事。
    [/quote]

    以下这段话是 The Java Programming Language 中描述overridding的:

    A method in a subtype potentially overrides an accessible method in a super type if the two methods have the same name and have override-equivalent signatures. We say "potentially overrides" because there is an additional requirement to be met: The signature of the subclass method must be the same as that of the superclass method, or it must be the same as the erasure of the signature of the superclass method. This constraint makes overriding a "one-way street": [b]A method without generic types can override a method with generic types, but not the other way around[/b].

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?