对Java中的单调度与多调度感到困惑

问题描述

我已经读过a good post

关于多调度(在Java中不是)与单调度(在Java中)的概念还有些困惑。

让我们使用以下示例:

class A { 
    m(A a) {
       System.out.println(“In A: “ + a.getClass());
    }
}
class B extends A {
    m(A a) {
       System.out.println(“In B.m(A): “ + a.getClass());
    }
    m(B a) {
       System.out.println(“In B.m(B): “ + a.getClass());
    }
}

A a = new A();
A b = new B();
B c = new B();

a.m(a);   // Java will call A.m(A); double-dispatch would call A.m(A)
b.m(b);   // Java will call B.m(A); double-dispatch would call B.m(B)
b.m(c);   // Java will call B.m(B); double-dispatch would call B.m(B)

•多次分派是否正确:

  1. 调用方法的对象的动态类型(因此,与Java单调度一样)
  2. 传递给方法的参数的动态类型(因此,显然与Java的单调度不同)

•因此,单调度和多调度之间的区别因此是上面的#2,其中单调度使用参数的静态类型而不是其动态类型?

非常感谢您提供任何见识

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)