Java中的绑定和分派有什么区别? [英] What is the difference between Binding and Dispatching in Java?

查看:220
本文介绍了Java中的绑定和分派有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有太多相关的名称:早期和晚期绑定,静态和动态调度,运行时与编译时多态等,我不明白它们之间的区别.

我找到了明确的解释,但是这是正确的吗?我将解释 JustinC :

绑定:正在确定变量的类型(对象?).如果它是在编译时完成的,则它的早期绑定.如果是在运行时完成的,那就是后期绑定.

调度:正在确定与该方法调用匹配的方法.静态调度是在编译时进行计算的方法,而动态调度是在运行时进行计算.

绑定是否将原始变量和引用变量分别与原始值和对象匹配?

请给我一些清晰的参考资料,以便我可以阅读更多有关此内容.

解决方案

我认为,这种混乱通常来自这些术语的重载.

我们用高级语言编写程序,而编译器或解释器必须将其转换为机器可以实际理解的东西.

粗略地讲,您可以描绘出一个编译器,它将我们的方法代码转换为某种形式的机器代码.如果编译器在稍后运行程序时准确知道该方法在内存中的位置,则它可以安全地找到该已编译方法的每个方法调用,并用跳转到已编译代码的该地址替换它住吧?.

嗯,实现这种关系是我理解的约束力.但是,这种绑定可能在不同的时刻发生,例如在编译时,链接时,加载时或在运行时,这取决于语言的设计.

静态和动态这两个术语通常分别用于指代在运行时之前和运行时绑定的事物.

以后的绑定时间与更大的灵活性相关联,更早的绑定时间与更高的效率相关联.语言设计师在创建语言时必须权衡这两个方面.

大多数面向对象的编程语言都支持子类型多态性.在这些语言中,虚拟方法是在运行时绑定的,具体取决于那时对象的动态类型.换句话说,虚拟方法是在运行时根据所涉及的对象实现的动态类型而不是仅基于其静态类型引用而分派给适当的实现的.

因此,我认为,您必须首先将方法调用绑定到特定的实现或执行地址等,然后可以将调用分派给它.

过去我已经回答一个非常相似的问题,在该示例中,我通过示例演示了Java中这是如何发生的./p>

我还建议您阅读本书编程语言实用程序.从理论的角度学习所有这些东西是很好的参考.

There are too many associated names: Early and Late Binding, Static and Dynamic Dispatch, Runtime vs. Compile-time Polymorphism, etc. that I don't understand the difference.

I found a clear explanation, but is it correct? I'll paraphrase JustinC:

Binding: is determining the type of a variable (object?). If it's done at compile time, its early binding. If it's done at run time, it's late binding.

Dispatch: is determining which method matches the method call. Static Dispatch is computing methods at compile time, whereas dynamic dispatch is doing it at run time.

Is Binding matching up primitive and reference variables with primitive values and objects respectively?

Edit: Please give me some clear reference material so I can read more about this.

解决方案

I believe the confusion typically comes from how overloaded these terms are.

We program our programs in a high level language, and either a compiler or an interpreter must transform that into something a machine actually understands.

In coarse terms, you can picture a compiler transforming our method code into some form of machine code. If the compiler knew at that point exactly where in the memory that method would reside when we run our program later, then it could safely go and find every method invocation of this compiled method and replace it with a jump to this address where the compiled code resides, right?.

Well, materializing this relationship is what I understand as binding. This binding, though, could happen at different moments, for example at compile time, linking time, load time, or at run time depending on the design of the language.

The terms static and dynamic are generally used to refer to things bound before run time and at run time, respectively.

Later binding times are associated with greater flexibility, earlier binding times are associated with greater efficiency. Language designers have to balance these two aspects when they're creating a language.

Most object-oriented programming languages support subtype polymorphism. In these languages, virtual methods are bound at runtime depending on the dynamic type of the object at that point. In other words, virtual methods are dispatched to the appropriate implementation at runtime based on the dynamic type of the object implementation involved and not based solely on its static type reference.

So, in my opinion, you must first bind the method invocation to a specific implementation or execution address, etc, and then you can dispatch an invocation to it.

I had answered a very similar question in the past in which I demonstrate with examples how this happens in Java.

I would also recommend reading the book Programming Language Pragmatics. It is a great reference to learn all this kind of stuff from a theoretical standpoint.

这篇关于Java中的绑定和分派有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆