简体   繁体   English

运行时动态绑定和类继承之间的区别

[英]Difference between runtime dynamic binding and class inheritance

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++). 我试图澄清动态语言(Python,ruby)和静态类型语言(java,C ++)中运行时动态绑定和类继承的概念。 I am not sure I am right or not. 我不确定我是对的。

In dynamic languages like Python and Ruby, runtime dynamic binding is implemented as duck typing. 在Python和Ruby等动态语言中,运行时动态绑定实现为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. 类继承只是减少Python和Ruby中的代码副本。

In static typed languages like Java and C++, runtime dynamic binding can be obtained only class inheritance. 在Java和C ++等静态类型语言中,运行时动态绑定只能获得类继承。 Class inheritance not only reduces code copy here, but is also used to implement runtime dynamic binding. 类继承不仅减少了代码副本,还用于实现运行时动态绑定。

In summary, class inheritance and runtime dynamic binding are two difference concepts. 总之,类继承和运行时动态绑定是两个不同的概念。 In Python and Ruby, they are totally different; 在Python和Ruby中,它们完全不同; in Java and C++ they are mixed together. 在Java和C ++中,它们混合在一起。

Am I right? 我对吗?

You are correct in that runtime dynamic binding is entirely different conceptually from class inheritance. 你是正确的,因为运行时动态绑定在概念上与类继承完全不同。

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." 但是当我重新阅读你的问题时,我认为我不同意“Java和C ++,运行时动态绑定是作为类继承实现的”。 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 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. 编辑我需要澄清一点:Java在重写的类方法上实现动态绑定,而C ++在运行时通过多态来确定一个类型,因此我不能说动态绑定与类继承“没有关系” 。 At a "macro" level, they're not inherently related, but a given language might leverage it in its inheritance mechanism. 在“宏”级别,它们本身并不相关,但是给定的语言可能会在其继承机制中利用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM