What is encapsulation inheritance polymorphism? Encapsulation inheritance Polymorphism
Encapsulation, inheritance, and polymorphism are not proposed for the C # language. They are a concept created under the object-oriented thinking. To understand encapsulation, inheritance, and polymorphism, you must first understand the object-oriented concept.
Encapsulation: How does this object come when we mention object-oriented? It is achieved through human encapsulation. Encapsulation is to combine some features or functions into an abstract object. For example, a computer has a display, a host, a mouse, and a keyboard. Functions include computing. When we combine these features and functions into the abstract term "computer", "computer" then has these features and functions. However, this "computer" is not a physical object. It represents the names of all physical objects named "computer. In C #, we call the encapsulated abstract term "class". Therefore, we call "computer" a class ), the physical computer calls this class Object (or instance ). Inheritance and polymorphism are the characteristics of the class.
Inheritance: This word can be interpreted in normal semantics. For example, you inherit the advantages of your father. In C #, classes can also be inherited (single inheritance). For example, we have a "computer" class. Now we define another class called "Lenovo computer ", do we need to redefine the features and functions of computers? Yes, but on the one hand, the Code is a bit repetitive, and on the other hand, it is not convenient to manage and describe the characteristics of "Lenovo computer. Therefore, we can make "Lenovo computer" inherit from the "computer" class. We call "Lenovo computer" as a subclass or a derived class of "computer, A computer is called a parent class or a base class. In this way, as long as "computer" and "Lenovo computer" are available, "Lenovo computer" can also evolve (and then add) something unique to itself. Therefore, the parent class (base class) is a subset of the subclass (derived class.
Polymorphism: a technique that allows you to set a parent object to be equal to one or more of its sub-objects, the parent object can operate in different ways based on the features of the sub-objects assigned to it. To put it simply, you can assign a pointer of the subclass type to a pointer of the parent class.
There are two methods to achieve polymorphism: overwrite and reload.
- Override refers to the method by which the subclass redefines the virtual function of the parent class.
- Overload means that multiple functions with the same name are allowed, and the parameter tables of these functions are different (maybe the number of parameters is different, maybe the parameter types are different, or both of them are different ).
In fact, the concept of overloading is not "Object-Oriented Programming". The implementation of overloading is that the compiler modifies the name of a function with the same name based on different parameter tables of the function, then these functions with the same name become different functions (at least for the compiler ).
For example, there are two functions with the same name: function func (p: integer): integer; and function func (p: string): integer ;. The modified function names of the compiler may be: int_func and str_func. For the call of these two functions, it has been determined between the compilers that it is static (remember: It is static ). That is to say, their addresses are bound during compilation (early binding). Therefore,Overload is not related to Polymorphism! "Overwrite" is related to polymorphism ". When the subclass redefined the virtual function of the parent class, the parent class pointer is dynamic based on the different subclass pointer assigned to it (remember: Dynamic !) The call of this function belongs to the subclass. Such a function call cannot be determined during compilation (the address of the virtual function of the subclass called cannot be provided ). Therefore, the function address isBinding during runtime(Late binding ). The conclusion is that overload is only a language feature and is irrelevant to polymorphism and object orientation!Reference Bruce Eckel: "Do not be stupid. If it is not late binding, it is not a polymorphism ."
So what is the role of polymorphism? We know that encapsulation can hide implementation details to modularize the code. inheritance can expand existing code modules (classes). They are all for the purpose --Code reuse. Polymorphism is to achieve another purpose --Interface reuse! Polymorphism is used to ensure that a certain attribute of an instance of any type in the family tree is called correctly when the class is inherited and derived.
Note: (this article from more reprint 1.: http://www.nowamagic.net/librarys/veda/detail/1835)
(This article from more reprint 2.: http://zhidao.baidu.com/link? Url = response)