gpt4 book ai didi

java - jls。返回类型可替换。这是什么意思?

转载 作者:行者123 更新时间:2023-11-30 03:54:30 25 4
gpt4 key购买 nike

我正在阅读 jls 并遇到以下术语:

return-type-substitutable

来自 jls 的片段

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 void then R2 is void.

If R1 is a primitive type then R2 is identical to R1.

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.

--R1 can be converted to a subtype of R2 by unchecked conversion (§5.1.9).

--d1 does not have the same signature as d2 (§8.4.2), and R1 = |R2|.

前两点很清楚。

你能澄清一下吗

--R1, adapted to the type parameters of d2 (§8.4.4), is a subtype of R2.

--R1 can be converted to a subtype of R2 by unchecked conversion (§5.1.9).

--d1 does not have the same signature as d2 (§8.4.2), and R1 = |R2|.

谢谢

附注路易吉·门多萨

interface Foo {
List<String> foo(String arg1, String arg2);
}

class Bar implements Foo {
@Override
public ArrayList<String> foo(String arg1, String arg2) {
//implementation...
return null;
}

public String foo(String arg1, String arg2, String arg3) {
//implementation...
return null;
}
}

它是工作变体。

我的问题的原因 - 我想理解 jls 中的以下短语:

If a method declaration d1 with return type R1 overrides or hides the declaration of another method d2 with return type R2, then d1 must be return-type-substitutable (§8.4.5) for d2, or a compile-time error occurs

规则:

If R1 is a reference type then **one of the following** is true:
...
--d1 does not have the same signature as d2 (§8.4.2), and R1 = |R2|.
...

代码:

interface Foo {
List<String> foo(String arg1, String arg2);
}

class Bar implements Foo {
@Override
public List<String> anotherName(String arg1, String arg2,Object obj) {
return null;
}

编译错误。

R1==R2 (List<String > == List<String>)

d1!=d2

我哪里违反了规则?

最佳答案

让我们拥有这个界面

interface Foo {
List<String> foo(String arg1, String arg2);
}

以及一个实现它的类

class Bar implements Foo {
@Override
public List<String> foo(String arg1, String arg2) {
//implementation...
}
}

我们有:

  • Bar#food1
  • List<String>将 d1 中的返回类型作为 R1。
  • Foo#food2
  • List<String>将 d2 中的返回类型作为 R2。

R1, adapted to the type parameters of d2 (§8.4.4), is a subtype of R2.

这意味着R1可以是 R2 的子类型,这意味着 R1 应通过 IS-A 测试。因此,我们可以执行以下操作:

class Bar implements Foo {
@Override
public ArrayList<String> foo(String arg1, String arg2) {
//implementation...
}
}

R1 can be converted to a subtype of R2 by unchecked conversion (§5.1.9).

这与泛型更为相关。这意味着R1即使它针对未经检查的覆盖发出警告,也应该通过 IS-A 测试。因此,我们可以执行以下操作:

class Bar implements Foo {
@Override
public ArrayList foo(String arg1, String arg2) {
//implementation...
}
}

d1 does not have the same signature as d2 (§8.4.2), and R1 = |R2|

这意味着重载:

class Bar implements Foo {
@Override
public ArrayList<String> foo(String arg1, String arg2) {
//implementation...
}

public ArrayList<String> foo(String arg1, String arg2, String arg3) {
//implementation...
}
}

关于java - jls。返回类型可替换。这是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23593946/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com