什么是java中的调度

我对确切的调度感到困惑。 特别是在双重调度方面。 有没有一种简单的方法可以掌握这个概念?

Dispatch是语言链接调用函数/方法定义的方式。

在java中,一个类可能有多个具有相同名称但参数类型不同的方法,并且该语言指定使用具有实际参数可以匹配的最具体类型的正确数量的参数将方法调用分派给该方法。 那是静态派遣。

例如,

void foo(String s) { ... } void foo(Object o) { ... } { foo(""); } // statically dispatched to foo(String) { foo(new Object()); } // statically dispatched to foo(Object) { foo((Object) ""); } // statically dispatched to foo(Object) 

Java也有虚拟方法调度。 子类可以覆盖超类中声明的方法。 因此,在运行时,JVM必须将方法调用分派给适合其运行时类型的方法版本。

例如,

 class Base { void foo() { ... } } class Derived extends Base { @Override void foo() { ... } } { new Derived().foo(); } // Dynamically dispatched to Derived.foo. { Base x = new Base(); x.foo(); // Dynamically dispatched to Base.foo. x = new Derived(); // x's static type is still Base. x.foo(); // Dynamically dispatched to Derived.foo. } 

Double-dispatch静态运行时 (也称为动态 )调度的组合。