当多个重载合法时会调用哪个方法?

发布于 2024-10-18 08:02:43 字数 250 浏览 1 评论 0原文

假设您有一个接口 A 和一个接口 B。假设 Sprite 类实现了这两个接口。

假设另一个类有一个方法 foo(A object),并且还有一个方法 foo(B object)

当我将 Sprite 实例传递给 foo() 方法时,两者都会被调用吗?如果不是,哪个优先?

Say you have an Interface A, and an Interface B. Let's say the Sprite class implements both interfaces.

Say there's another class that has a method foo(A object), and also has a method foo(B object).

Do both get called when I pass an instance of Sprite to the method foo()? If not, which has precedence?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

南七夏 2024-10-25 08:02:43

使用方法重载(如此处使用的那样),要调用的方法将在编译时根据保存 Sprite 的变量(声明的)类型来解析。

由于方法调用不明确,因此在您手动向下转换引用以解决不明确性之前,这将无法编译。

With method overloading (as is in use here), the method to call is resolved at compile time, based on the (declared) type of the variable holding the Sprite.

Since the method call is ambiguous, this will fail to compile until you manually downcast the reference to resolve the ambiguity.

无法言说的痛 2024-10-25 08:02:43

这是模棱两可的。您需要转换到其中一个接口。

It's ambiguous. You'll need to cast to one of the interfaces.

七婞 2024-10-25 08:02:43
   interface A {}

   interface B {}

   class Sprite implements A,B {}

   class Test{
      void foo(A a){}
      void foo(B b){}

      void test(){
       Sprite s = new Sprite();
       foo(s); // <-- compile time error (The method foo(A) is ambiguous for the type Test)
      }
   }
   interface A {}

   interface B {}

   class Sprite implements A,B {}

   class Test{
      void foo(A a){}
      void foo(B b){}

      void test(){
       Sprite s = new Sprite();
       foo(s); // <-- compile time error (The method foo(A) is ambiguous for the type Test)
      }
   }
定格我的天空 2024-10-25 08:02:43

一般答案需要好几页才能解释;请参阅语言中的相关部分规范

在您提出的具体情况下,它只是无法编译。但在其他情况下,编译器可能会根据一些规则更喜欢一种重载而不是另一种重载并解决冲突。

最重要的规则是,一般来说,如果一种方法比其他方法更具体,则选择该方法。如果方法 A 的所有可能调用在调用方法 B 时也能编译,则方法 A 比方法 B 更具体。这通常意味着方法 A 的某些形式参数是从相应的方法 A 派生的子类。方法 B 中的形式参数。在某些情况下,这不适用,例如当需要自动装箱或其他“方法转换”以使参数适合其他“更具体”的情况时。具体请参见规格。

The general answer requires several pages to explain; see the relevant section in the language specification.

In the specific case you present, it just won't compile. But in other cases there are rules by which the compiler may prefer one overload to another and resolve the conflict.

The most important rule is that, in general, if one method is more specific than the others, that method is picked. Method A is more specific than method B if all possible invocations of method A would also compile if calling method B. This usually means that some of the formal parameter(s) of method A are subclassed from the corresponding formal parameter(s) in method B. There are a few cases where this won't apply, such as when auto-boxing or other "method conversion" would be required to make an argument fit in the otherwise "more specific" case. For specifics, see the spec.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文