java 什么是动态方法分派,它与继承有何关系?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4343247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 05:58:09  来源:igfitidea点击:

What is dynamic method dispatch and how does it relate to inheritance?

java

提问by Subhransu Mishra

What is meant by dynamic dispatchin Java, and why do we need it in the context of inheritance?

Java 中的动态调度是什么意思,为什么我们在继承的上下文中需要它?

回答by aioobe

What is meant by dynamic dispatchin Java […]?

Java [...] 中的动态调度是什么意思?

Think of "dispatch" as "determining which method to call".

将“调度”视为“确定要调用的方法”。

The "dynamic" part simply says that it is determined at runtime. That is, which method is to be called is determined at runtime.

“动态”部分只是说它是在运行时确定的。也就是说,要调用哪个方法是在运行时确定的。

why do we need it in the context of inheritance

为什么我们在继承的上下文中需要它

Without inheritance / polymorphism we wouldn't need this. The type of an expression would be decidable at compile time, and which method that would have been called at runtime would be known when compiling the program.

如果没有继承/多态,我们就不需要这个。表达式的类型在编译时是可确定的,并且在编译程序时将知道在运行时调用的方法。

With inheritance / polymorphism we don't know the concrete runtime type of an expression, thus which method to be called must be "dynamically" determined during runtime.

对于继承/多态,我们不知道表达式的具体运行时类型,因此必须在运行时“动态”确定要调用的方法。

Without dynamic dispatch, virtual methods wouldn't make sense, which is central for abstraction and encapsulation.

没有动态调度,虚方法就没有意义,这是抽象和封装的核心。

Recommended reading: Wikipedia article on Dynamic Dispatch

推荐阅读:维基百科关于动态调度的文章

回答by Ralph

Other Answers discus the theory, this is an example to show why dynamic dispatch (also called late binding) is needed.

其他答案讨论了理论,这是一个示例,说明为什么需要动态调度(也称为后期绑定)。

Assume you have an Class 'Rectangle`.

假设您有一个“矩形”类。

public class Rectangle{
  public void draw() {
     System.out.println("___\n| |\n---");
     //does it look nice?
  }
}

And you have a subclass with rounded corners

你有一个带圆角的子类

public class RoundedRectangle extends Rectangle {
  @Override
  public void draw() {
     System.out.println("assume it is a rectangle with rounded corners");
  }
}

Now assume you have a method getting with an Parameter of Type Rectangle, which is used in the method to invoke the draw method

现在假设您有一个获取矩形类型参数的方法,该方法用于调用 draw 方法

public class Demo() {
...
  public demonstration(Rectangle rect) {
    rect.draw();
  }
...
}

It the argument of this method is of class Rectangle then it will draw

如果这个方法的参数是类 Rectangle 那么它将绘制

___
| |
---

But when the argument is of type RoundedRectangle, then we expect:

但是当参数是 RoundedRectangle 类型时,我们期望:

assume it is a rectangle with rounded corners

And this is where late binding is needed for: When the code compiles, it is not clear which method needs to be invoked by rect.draw(); I could be Rectangle.draw(), RoundedRectangle.draw(), or the draw method of any other yet not implemented subclass of Rectangle.

这就是需要后期绑定的地方:当代码编译时,不清楚需要通过 rect.draw() 调用哪个方法;我可以是 Rectangle.draw()、RoundedRectangle.draw() 或任何其他尚未实现的 Rectangle 子类的 draw 方法。

Summary (from the view of an developer):

总结(从开发者的角度来看):

So late binding means: invoke the method of given signature (Method name, and argument list) for an instance where is only known that it is of an specific class or subclass - but it is not known which class it is exactly. And when a method is overriden, then ivoke the overriding method.

所以后期绑定意味着:为一个实例调用给定签名(方法名称和参数列表)的方法,其中只知道它属于特定类或子类 - 但不知道它到底是哪个类。当一个方法被覆盖时,调用覆盖的方法。

So there is no Polymorphism vs. Late Binding -- It is you need Late Binding when you have Polymorphism. In Java and in C++ (and i belive, in every other Object Oriented language too)

所以没有多态与后期绑定——当你有多态时,你需要后期绑定。在 Java 和 C++ 中(我相信,在所有其他面向对象的语言中也是如此)

回答by user207421

What is dynamic method dispatch in case of java

在java的情况下什么是动态方法调度

Same as it is in any other language. Despatching dynamically to a method chosen on the basis of the type of the object the method is invoked on.

与任何其他语言相同。动态调度到根据调用方法的对象类型选择的方法。

and why do we need that in case of inheritance(what is the need of DMD)

以及为什么在继承的情况下我们需要它(DMD 需要什么)

That's the only time you doneed it. If you don't have inheritance there is nothing to be dynamic about.

这是你唯一的一次需要它。如果您没有继承,则没有什么可动态的。

Which one is better Polymorphism in C++ or DMD in java.

C++ 中的多态性或 Java 中的 DMD 哪个更好。

The question is meaningless, as they aren't different. DMD is a mechanism for implementing one aspect of polymorphism.

这个问题没有意义,因为它们没有什么不同。DMD 是一种实现多态性的一个方面的机制。