本文介绍了动态,静态和晚期绑定有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在浏览。在动态 - >动态绑定的主题下,有三个概念:

I was going through Object-Oriented Programming in Objective-C guide by Apple. Under the topic of Dynamism-->Dynamic Binding, there were three concepts:


  1. 动态绑定

  2. 延迟绑定

  3. 静态绑定

我几乎理解了动态和晚期绑定,但静态绑定困惑很多。有人可以用Objective-C或C ++中的示例解释这三个概念之间的区别吗?

I've almost understood the difference(s) between Dynamic and Late binding, but Static Binding confuses a lot. Can somebody please explain the differences between these three concepts with examples either in Objective-C or C++?

注意:在将此问题标记为重复之前,问题,同时描述这三个事情。

Note: Before you think to mark this question as duplicate, there is no question on SO that describes these three things simultaneously.

推荐答案

静态绑定:在编译时确定要引用的项目。在(Objective-)C(++)中,对函数的调用是静态绑定的;例如库函数 c> getBackground 方法被调用,直到运行时才被确定,并且依赖于值的类型 t 正在引用。但是必须是 getBackground 方法,因为 t 只能引用类型为 TextComponent 或其中一个子类型。事实上,当编译器编译这个片段 TextComponent 的所有子类型可能不是已知的,但没有关系,因为子类型保证每一个将有一个 getBackground 方法。因此,像静态绑定一样,后期绑定在运行时不会失败。

Which getBackground method is called is not determined until runtime and is dependent on the type of value t is referencing. However there must be a getBackground method, as t can only reference values whose type is TextComponent or one of its subtypes. Indeed when the compiler compiles this fragment all the subtypes of TextComponent may not be known, but it doesn't matter as subtyping guarantees that every one will have a getBackground method. So, like static binding, late binding cannot fail at runtime.

动态绑定:这是一个超出后期绑定的步骤,运行时来确定引用项是否存在。 Objective-C中的一个简单示例:

Dynamic binding: This is a step beyond late binding where it is left to runtime to determine if the referenced item exists. A simple example in Objective-C:

id anyObject; // this can hold a reference to any Objective-C object

NSUInteger len = [anyObject length]; // TRY to call a method length on the object
                                     // referenced by anyObject. If at runtime this is,
                                     // say, an NSString or NSMutableString value it will
                                     // succeed. However if it is, say, an NSNumber value
                                     // it will FAIL

绑定,实际调用的方法直到运行时才被确定;然而,不像后期绑定,这样的方法是否存在也留给运行时。因此,与静态和晚期绑定不同,动态绑定可能会在运行时失败

Like late binding, the actual method that gets called is not determined until runtime; however, unlike late binding, whether such a method exists is also left to runtime. So, unlike both static and late binding, dynamic binding can fail at runtime.

许多语言不支持动态绑定,由于运行时错误的可能性 - 它不受欢迎的安全关键软件,但是是快速原型。然而,它确实允许更难以用其他方式完成的设计,所以一些语言,如Objective-C,这些语言的用户必须承认,权力是责任。 Xcode / Clang编译器尽可能地进行尽可能多的静态类型检查,大多数Objective-C代码将尽可能精确的类型。

As you might ask: Many languages do not support dynamic binding at all, due to the possibility of runtime error - it is not favoured for safety critical software, but is for rapid prototyping. However it does allow designs which are much harder to accomplish in other ways so some languages do, like Objective-C, and users of those languages have to accept that with power comes responsibility. The Xcode/Clang compiler goes to great lengths to do as much static type checking as possible, and most Objective-C code will be as precise as possible with types.

这篇关于动态,静态和晚期绑定有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 10:50