简体   繁体   English

Java类有两个具有相同函数签名但返回类型不同的方法

[英]Java class has 2 methods with the same function signature but different return types

AFAIK it's not possible to have a method with the same call signature. AFAIK不可能有一个具有相同呼叫签名的方法。 However: 然而:

$ javap -public java.time.LocalTime  | grep "minus" | grep "Temporal" | grep -v "long"
    public java.time.LocalTime minus(java.time.temporal.TemporalAmount);
    public java.time.temporal.Temporal minus(java.time.temporal.TemporalAmount);

These clearly show multiple methods with the same call signature. 这些清楚地显示了具有相同呼叫签名的多种方法。

  1. How does Java resolve the function call? Java如何解析函数调用?
  2. Why are there multiple functions? 为什么有多种功能?

EDIT: Simplified the question by keeping only the relevant bit. 编辑:通过仅保留相关位简化问题。

This is due to how Java implements covariant return types. 这是由于Java如何实现协变返回类型。 java.time.LocalTime has a minus method with signature java.time.LocalTime有一个带签名的minus方法

LocalTime minus(TemporalAmount amountToSubtract)

but this method implements an interface method from java.time.temporal.Temporal with signature 但是这个方法实现了java.time.temporal.Temporal的签名接口方法

Temporal minus(TemporalAmount amount)

This is permitted due to covariant return typing, but due to the way method lookup works, a lookup at runtime for the method that returns a Temporal won't find the method that returns a LocalTime . 由于协变返回类型,这是允许的,但由于方法查找的工作方式,在运行时查找返回Temporal的方法将找不到返回LocalTime的方法。 Thus, the compiler creates an ordinarily-forbidden method with the same signature, but returning a Temporal . 因此,编译器创建一个具有相同签名的普通禁止方法,但返回Temporal This method calls the version that returns a LocalTime . 此方法调用返回LocalTime的版本。 At runtime, calls that want a Temporal return type find the bridge method, and everything works out. 在运行时,想要Temporal返回类型的调用找到桥接方法,一切都解决了。

This bridge method is normally invisible, but it shows up in the javap output, leading to your current confusion. 这种桥接方法通常是不可见的,但它出现在javap输出中,导致您当前的混淆。

Source: http://www.artima.com/weblogs/viewpost.jsp?thread=354443 资料来源: http//www.artima.com/weblogs/viewpost.jsp? thread = 354443

Here's the javap -c disassembly for one of the bridge methods from StringBuilder , showing how it calls the method with the same signature, but a more specific return type: 这是来自StringBuilder一个桥接方法的javap -c反汇编,显示了它如何使用相同的签名调用该方法,但更具体的返回类型:

  public java.lang.Appendable append(java.lang.CharSequence) throws java.io.IOException;
    Code:
       0: aload_0       
       1: aload_1       
       2: invokevirtual #6                  // Method append:(Ljava/lang/CharSequence;)Ljava/lang/StringBuilder;
       5: areturn       

Java forbids two methods in the same class or interface with the same name and signature . Java禁止在具有相同名称和签名的同一类或接口中使用两种方法

However, two different classes, interfaces, or enums can have the same method signature; 但是,两个不同的类,接口或枚举可以具有相同的方法签名; for example, this is valid: 例如,这是有效的:

public java.time.LocalTime minus(long, java.time.temporal.TemporalUnit);
public java.time.temporal.Temporal minus(long, java.time.temporal.TemporalUnit);

One minus method belongs to the LocalTime class, and the other belongs to the Temporal interface. 一个minus属于LocalTime类,另一个属于Temporal接口。 Since LocalTime implements Temporal , there must be a signature match between those two or the contract is not fulfilled, which would lead to a compilation error. 由于LocalTime实现Temporal ,因此必须在这两者之间存在签名匹配,否则合同将不会完成,这将导致编译错误。

Temporal是由LocalTime实现的接口,因此如果要检查尝试,它们的方法具有相同的签名

javap java.time.LocalTime

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java 8 - 两个接口包含具有相同方法签名但返回类型不同的默认方法,如何覆盖? - Java 8 -Two interfaces contain default methods with the same method signature but different return types, how to override? JAVA 方法在同一个类和同一个实现中但是不同的对象类型作为参数 - JAVA methods in same class and same implementation but different object types as parameter 接口(两种方法,不同的返回类型取决于类)Java - Interface (two methods, different return types depending on class) Java 命名具有不同返回类型但功能相同的方法 - naming methods with different return types but the same functionality 函数Java的不同返回类型 - Different return types for function Java Java不同的函数返回类型 - Java different function return types 两个接口中具有相同签名但返回类型不同的方法 - Methods with the same signature but different return type in two interfaces 为什么 AKKA TypedActor class 有两个签名相同的方法? - Why the AKKA TypedActor class has two methods with the same signature? Java中的返回类型和方法 - Return types and Methods in java Extends 和 Interface 具有相同的方法,但具有相同的参数但不同的返回类型 - Extends and Interface has same method with the same parameters but different return types
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM