可能重复:
这是有效的Java吗?
我惊讶地发现下面的Java类编译.它有几个方法,具有相同的名称,参数数量和以下类型 - 擦除类型的参数.然而,它使用各种版本的Sun JDK 1.6编译器在Windows上编译并按预期工作.所以,如果这是一个bug已经存在多年......
它还编译了许多版本的Eclipse,但不使用Eclipse 3.6附带的编译器进行编译
此外,调用代码按预期工作 - 即.关于调用代码的ambigious方法没有错误.
如果你迭代ErasureExample.class.getMethods()返回的方法,它们都存在.....
根据JLS,如果方法具有"覆盖等效"签名将是非法的 - 严格来说它们不会,因为Collection,Collection和Collection都不是覆盖等价的....如果Eclipse是错误的话,JDK正确...
功能还是错误?它应该编译吗?
/**
* Demonstrates that a class with methods that differ only by return type can exist.
* Section 8.4 of the JLS suggests this is an error IFF the methods had
* override equivalent signatures, which they dont''
*
*
* From JLS 8.4...
* It is a compile-time error for the body of a class to declare as members two methods
* …Run Code Online (Sandbox Code Playgroud)