gpt4 book ai didi

java - 不会导致编译时错误的多个最大特定方法的示例

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:23:44 24 4
gpt4 key购买 nike

我需要深入了解 Java 中方法调用的细节,同时阅读 Choosing the Most Specific Method 部分在 Java Language Specification (Java SE 12 Edition) 中,我发现在调用多个方法期间(1) 可以最大程度地特定并且(2) 拥有多个最具体的方法并不总是会导致编译时错误。

我能够想到一个例子,其中两种方法都非常具体:

interface A {}

interface B {}

class C implements A, B {
<T extends A> void foo(T t) {};
<T extends B> void foo(T t) {};
}

class Main {
public static void main(String[] args) {
new C().<C>foo(null);
}
}

此示例导致编译时错误:error: reference to foo is ambiguous

这对我来说很有意义,但对我来说没有意义的是,当有多个最具体的方法时,它不会导致编译时错误。

本节Choosing the Most Specific Method Java Language Specification (Java SE 12 Edition) 中提到了两种情况,当存在多个最大特定方法时,编译器能够选择一个方法:

  • If all the maximally specific methods have override-equivalent signatures (§8.4.2), and exactly one of the maximally specific methods is concrete (that is, neither abstract nor default), then it is the most specific method.

  • Otherwise, if all the maximally specific methods have override-equivalent signatures, and all the maximally specific methods are abstract or default, and the declarations of these methods have the same erased parameter types, and at least one maximally specific method is preferred according to the rules below, then the most specific method is chosen arbitrarily among the subset of the maximally specific methods that are preferred. The most specific method is then considered to be abstract.

首先,如何调用抽象的方法?为什么要将抽象方法用于方法调用?

其次,有人可以为这两种情况中的每一种提供一个不会导致编译时错误的示例吗?

最佳答案

I found that (1) during invocation multiple methods can have the same signature and that (2) having multiple methods with the same signature doesn't always result in a compile-time error.

一个类不能包含两个具有相同签名的方法。

8.4.2. Method Signature

Two methods or constructors, M and N, have the same signature if they have the same name, the same type parameters (if any) (§8.4.4), and, after adapting the formal parameter types of N to the the type parameters of M, the same formal parameter types.

The signature of a method m1 is a subsignature of the signature of a method m2 if either:

  • m2 has the same signature as m1, or

  • the signature of m1 is the same as the erasure (§4.6) of the signature of m2.

Two method signatures m1 and m2 are override-equivalent iff either m1 is a subsignature of m2 or m2 is a subsignature of m1.

It is a compile-time error to declare two methods with override-equivalent signatures in a class.

在您的示例中,有两种方法具有两种不同的签名。它编译并工作正常,除非你引入像 new C().<C>foo(null); 这样的歧义。 .编译时错误“对 foo 的引用不明确”并不意味着 <T extends A> void foo(T t)<T extends B> void foo(T t)不能共存。他们实际上可以,而且正在做。

如评论中所述,类型删除后,方法将如下所示

 void foo(A t);
void foo(B t);

How is it possible to invoke a method that is abstract? Why would an abstract method ever be considered for method invocation?

在抽象上下文中(例如在抽象类中)调用抽象方法绝对没问题。

Can someone provide an example for each of these two scenarios that don't result in compile-time errors?

我可以想到一个示例,其中有两个“具有覆盖等效签名的最具体方法”用于调用 new C().foo();并且正在成功解决 A的方法。

abstract class A {
public void foo() {
System.out.println("a");
}
}
interface B {
default void foo() {
System.out.println("b");
}
}
class C extends A implements B {
public static void main(String[] args) {
new C().foo(); // prints a
}
}

关于java - 不会导致编译时错误的多个最大特定方法的示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57836517/

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