[英]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. 这些清楚地显示了具有相同呼叫签名的多种方法。
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.