Java方法重载错误

标签 java jakarta-ee

我有这个 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/

相关文章:

java - Play Framework (2.4)、sbt (0.13.17) 的热重载以及整个项目完全重新编译的原因

java - 启用 Jasper Studio Web 服务插件

jakarta-ee - 远程 EJB 调用上的事务

java - 创建多种类型的 jpa 实体

java - 如何在 Jetty 上设置 Jersey

使用 JSON Adventure 将 Javascript 数组转换为 Java 数组

java - 发送数据时,在 Android 应用程序中从 Firebase 检索 ServerValue.Timestamp

java - 应该如何将 tomcat 与现有项目一起使用?

jakarta-ee - 接受 Java 中的自签名证书

java - Lucee,在 Windows 上,在第一次请求应用程序时附加 index.cfm