0

我读过一篇好文章

只是对多分派(不是在 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,其中单调度使用参数的静态类型而不是动态类型?

感谢您的任何见解

4

0 回答 0