我有这个 getResult(),输入参数略有不同。一个 Mapvalue 作为 one 的字符串数组和 anor 的字符串。
但是我看到一个错误: 不会重载支持泛型吗?
public class App
{
public void getResult(Map<String, String[]> map) {
}
public void getResult(Map<String, String> map) {
}
我看到这个错误:方法 getResult(Map) 与类型 App 中的另一个方法具有相同的删除 getResult(Map) 我猜 java 将两者都视为 map ,所以这个错误也是如此。
模仿这种行为的最佳方法是什么?
谢谢
最佳答案
重载的规则之一是带有类型参数的方法在删除后不能有相同的签名。参见 the Java Language Specification, §8.4.8.3 .规范中的相关文本是:
It is a compile-time error if a type declaration T has a member method m1 and there exists a method m2 declared in T or a supertype of T such that: . . . The signature of m1 or some method m1 overrides (directly or indirectly) has the same erasure as the signature of m2 or some method m2 overrides (directly or indirectly).
关于Java方法重载错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11908758/