What is the difference between Dynamic, Static and Late binding?

元气小坏坏 提交于 2019-12-13 00:57:54

问题


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. Dynamic Binding
  2. Late Binding
  3. Static Binding

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.


回答1:


Static binding: is where the item being referred to is determined at compile time. In (Objective-)C(++) a call to a function is statically bound; for example the library functions fabs and NSLog. Also references to variables are static in these languages; which variable is being referenced is determined completely at compile time. Static binding cannot fail at runtime, it is a compile time error to fail to determine what a statically bound reference refers to.

Late binding: This is where the exact item being referred is determined at runtime. This typically, but not always, comes about when a language supports inheritance/subtyping where a type T may be a subtype/child/subclass (terminology depends on language) of a type S. This means that a value of type T has all the attributes of a value of type S, and in object-oriented languages that includes methods, and that a value of type T can be treated as a value of type S. A Java example:

TextComponent t; // a reference to a value of type TextComponent OR any of
                 // its subclasses, such as TextArea or TextField
Color c = t.getBackground(); // a call to TextComponent's getBackground method or
                             // TextArea's getBackground method etc.

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.

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.

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.



来源:https://stackoverflow.com/questions/22427867/what-is-the-difference-between-dynamic-static-and-late-binding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!