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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?