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

假设如下代码:

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

[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].

先看看你后面所说的工具中的JRE版本

泛型的作用就是——不用强制转换。

 

知道泛型中的bridge method 吗?

 

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

因为I不使用泛型,I<C>才使用泛型,所以I的方法签名就是:

public void query(Map map, int count)

如果你在C中定义:

public void query(Map <String, Object> map, int count) {}

 

泛型的overridding是通过bridge method实现的。

这样I中的 方法:

public void query(Map map, int count)

就无法通过C中的方法:

public void query(Map <String, Object> map, int count) {}

实现。

也就是,C中有两个方法:

 public void query(Map <String, Object> map, int count) {
  ....
}

 //这个方法是编译自动添加的。   
bridge public void query(Map map, int count){    
     // 调用C中实现的方法实现父类的方法。由于Map<String,Object>的范围要比 Map小。
    //因此编译器就无法转换,因为泛型的一条规则:隐式的类型转化在任何时候都不能失败。
 query((Map <String, Object>) map,  count) ;                                                                                         
}

 

 

 

以上属于个人理解。

最近才仔细学习Generic,可能理解有误呀.

关键是两者的编译器实现不同。上面的程序可以直接在命令行用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="feng_gladys"]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].[/quote]

感谢feng_gladys指出这一点,不过我觉得楼主举的例子正是“A method without generic types can override a method with generic types”,因此是允许的。从前面的语句“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.”,意思是子类要么和父类方法签名完全相同,或者和类型擦除后的方法签名相同,楼主的例子满足后一种条件。