本文介绍了是协变量返回类型覆盖根据jls?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我相信所有的jls都是真理。



考虑来自jls的下一个片段:

转到子签名声明:

关于同一签名:

哪个规则允许使用协变返回类型?我看不到。



@ rgettman answer



 返回类型为R1的方法声明d1是返回类型可替换的, iff任何以下是真的:
...
- 如果R1是引用类型,则下列之一为真:
---- R1,适用于类型参数的d2(§8.4.4),是R2的一个子类型。
...

和子签名要求:

对我来说看起来像是矛盾。

解决方案

这些规则都不允许协变返回类型。方法的签名引用方法名称及其参数的类型和顺序,但不引用返回类型。



这是指,其中规定:

(强调我)



我加粗的部分允许协变返回类型。


just say, that point of my question is learn to understand jls.

I believe that all in jls is truth.

consider next snippet from jls:

go to subsignature declaration:

about same signature:

Which rule does allow to use covariant return type ? I don't see it.

P.S. after @rgettman answer

A method declaration d1 with return type R1 is return-type-substitutable for another method d2 with return type R2 iff any of the following is true:
...
--If R1 is a reference type then one of the following is true:
----R1, adapted to the type parameters of d2 (§8.4.4), is a subtype of R2.
...

and subsignature requirement:

For me it is looks like contradiction.

解决方案

Neither of these rules allows covariant return types. Signatures of a method refer to the method name and the types and order of its arguments, but not to the return type.

From JLS, Section 8.4.8.3:

That refers to Section 8.4.5, which states:

(emphasis mine)

That part I bolded allows covariant return types.

这篇关于是协变量返回类型覆盖根据jls?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 11:52