Java多态性:为什么这种方法称为最后一个?

发布于 2025-01-23 02:09:14 字数 1113 浏览 1 评论 0原文

我试图通过以下示例更好地理解Java中的多态性:

public class A {

1 int method(A a) { return 1; }
2 static int method(B b) { return 2 + b.method((A)b); }

}
public class B extends A { 

3 final int method(A a) { return 4 + super.method(a); }
4 int method(C c) { return 8 + this.method((B)c); }

}

public class C extends B {
5 int method(C c) { return 16 + method((A)c); }
}

And, the following declaration, initialization, methodcall:
B b = new B();
b.method(b);

我无法缠绕我的头,为什么“ int方法(a a){return 1;}”是最后一个要称呼的。

到目前为止,我的思路:

  1. 我们找出静态和动态类型以及B(b和b)的
  2. 静态类型(B和B),如果我们找到了一种称为方法的方法,该方法接受参数。 我们发现两个候选人:3,2。 我们称之为2,因为它不需要对静态B中的静态施法,而B的静态和动态类型是相同的。
  3. 现在找出B.Method((a)b)的Stat/dyn.types: b是(b/b),在铸件之后,参数中的b为(a/b)。现在,我们在B类中寻找一种称为方法的方法,该方法接受A-Type参数,我们找到一个,1,但是现在,因为我们有不同的动态类型(b),我们也需要搜索B。候选人,我们发现3,我们使用它,因为它覆盖了1。
  4. 在那之后,我们再次辨别了类型,因为“超级”静态类型是parenterclass(a)和动态的类型:b(b(引起超级呼叫的对象的动态类型)和a和a为其参数。
  5. 因此,现在我们在A中寻找接受A的方法,我们发现了1个,但是动态类是不同的,因此我们还需要检查B类中的候选人,该候选人可能会覆盖A(1)中发现的方法并使用它,如果是这种情况,这将导致我们再次调用方法3。

但是显然,这不是Java返回的,我不知道这种方法的错误在哪里。在其他多态性练习中,我提出了正确的解决方案。

i was trying to understand polymorphism in Java better with the following example:

public class A {

1 int method(A a) { return 1; }
2 static int method(B b) { return 2 + b.method((A)b); }

}
public class B extends A { 

3 final int method(A a) { return 4 + super.method(a); }
4 int method(C c) { return 8 + this.method((B)c); }

}

public class C extends B {
5 int method(C c) { return 16 + method((A)c); }
}

And, the following declaration, initialization, methodcall:
B b = new B();
b.method(b);

I can not wrap my head around, why the "int method(A a) { return 1; }", is the last one to be called.

My train of thought so far:

  1. We figure out the static and dynamic Type and of b (B and B)
  2. We look in class B, if we find a method called method, which accepts parameter with static type B.
    We find two candidates: 3, 2.
    We call 2, because it requires no implicit casting to the static Type B in the parameter and the static and dynamic types of B are the same.
  3. Now figure out the stat/dyn.types for b.method((A) b):
    b is (B/B), and b in the parameter is (A/B) after the cast. Now, we look in class B for a Method called method, which accepts an A-type parameter, we find one,1, but now, because we have a different dynamic type (B), we also need so search B for a possible candidate, we find 3, we use it, since it overrides 1.
  4. After that, we discern the types again, because of "super" the static type is the one from the parentclass (A), and the dynamic one: B (the dynamic type of the object which caused the super-call), and A and A for its parameter.
  5. So, now we seek in A for a method which accepts an A, we find 1, but the dynamic class is different, so we need to also check for candidates, in class B, which might overwrite the method found in A (1), and use it, if that is the case, which would lead us to calling method 3, again.

But obviously that is not what Java returns, and i do not know where the error to this approach is. In other polymorphism exercises, i came to the correct solution.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文