运行时动态绑定和类继承之间的差异 [英] Difference between runtime dynamic binding and class inheritance

查看:186
本文介绍了运行时动态绑定和类继承之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图澄清动态语言(Python,ruby)和静态类型语言(java,C ++)中运行时动态绑定和类继承的概念。我不确定我是否对。

I am trying to clarify the concept of runtime dynamic binding and class inheritance in dynamic languages (Python, ruby) and static type languages (java, C++). I am not sure I am right or not.

在动态语言(如Python和Ruby)中,运行时动态绑定实现为鸭式类型。当解释器检查对象的类型时,它检查对象是否具有特定方法(或行为),而不是检查对象的类型;而运行时动态绑定并不意味着类继承。类继承只是减少Python和Ruby中的代码复制。

In dynamic languages like Python and Ruby, runtime dynamic binding is implemented as duck typing. When the interpreter checks the type of an object, it checks whether the object has the specific method (or behaviour) rather than check the type of the object; and runtime dynamic binding does not mean class inheritence. Class inheritance just reduce code copy in Python and Ruby.

在Java和C ++等静态类型语言中,运行时动态绑定只能获得类继承。类继承不仅减少了代码复制,而且还用于实现运行时动态绑定。

In static typed languages like Java and C++, runtime dynamic binding can be obtained only class inheritance. Class inheritance not only reduces code copy here, but is also used to implement runtime dynamic binding.

总之,类继承和运行时动态绑定是两个不同的概念。在Python和Ruby中,它们是完全不同的;在Java和C ++中,它们混合在一起。

我是对吗?

推荐答案

你是正确的,因为运行时动态绑定在概念上与类继承完全不同。

You are correct in that runtime dynamic binding is entirely different conceptually from class inheritance.

但是当我重新阅读你的问题时,我不认为我会同意Java和C ++,运行时动态绑定实现为类继承。类继承仅仅是更广泛行为的定义,包括现有类的现有行为。此外,运行时绑定不一定与对象定向有任何关系;它只能引用延迟的方法解析。

But as I re-read your question, I don't think I would agree that "Java and C++, runtime dynamic binding is implemented as class inheritance." Class inheritance is simply the definition of broader behavior that includes existing behavior from existing classes. Further, runtime binding doesn't necessarily have anything to do with object orientation; it can refer merely to deferred method resolution.

类继承指的是如何构建对象的模板,具有越来越精致的行为与连续的子类。运行时动态绑定只是一种方法,例如对方法的引用(例如)推迟到执行时间。在给定的语言中,特定类可以利用运行时动态绑定,但是具有在编译时解析的继承类。

Class inheritance refers to the "template" for how an object is built, with more and more refined behavior with successive subclasses. Runtime dynamic binding is merely a way of saying that a reference to a method (for example) is deferred until execution time. In a given language, a particular class may leverage runtime dynamic binding, but have inherited classes resolved at compile time.

简而言之,继承是指对象的定义蓝图

In a nutshell, Inheritance refers to the definition or blueprint of an object. Runtime dynamic binding is, at its most basic level, merely a mechanism for resolving method calls at execution time.

EDIT 我需要澄清一下,在运行时动态绑定的最基本的层次上,一点是这样的:Java在重写的类方法上实现动态绑定,而C ++在运行时通过多态来确定类型,因此我不能说动态绑定与类无关遗产。在宏层面,它们不是固有的相关性,但是一种给定的语言可以在其继承机制中利用它。

EDIT I do need to clarify one point on this: Java implements dynamic binding on overridden class methods, while C++ determines a type through polymorphism at runtime, so it is not accurate for me to say that dynamic binding has "no relationship" to class inheritance. At a "macro" level, they're not inherently related, but a given language might leverage it in its inheritance mechanism.

这篇关于运行时动态绑定和类继承之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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