Three elements of object-oriented architecture: encapsulation, inheritance, polymorphism, and three elements Polymorphism

Source: Internet
Author: User

Three elements of object-oriented architecture: encapsulation, inheritance, polymorphism, and three elements Polymorphism

The three elements of object-oriented architecture are encapsulation inheritance polymorphism.

Encapsulation

Encapsulation is the abstraction of things as a class, exposing external interfaces, and hiding internal data.

InheritanceOne of the main functions of Object-Oriented Programming (OOP) is "inheritance ". Inheritance refers to the ability to use all the functions of an existing class and extend these functions without re-writing the original class. The new class created by inheritance is called a subclass or a derived class ". The inherited class is called "base class", "parent class", or "super class ". The process of inheritance is from general to special. To implement Inheritance, You can implement it through "Inheritance" and "Composition. In some OOP languages, a subclass can inherit multiple base classes. However, in general, a subclass can only have one base class. To implement multi-inheritance, You can implement multi-level inheritance. Inheritance concepts can be implemented in three ways: Implementation inheritance, interface inheritance, and visual inheritance. Implementation Inheritance refers to the ability to use the attributes and methods of the base class without additional encoding; Ø interface inheritance refers to the ability to only use the names of attributes and methods, but the subclass must provide implementation; visual Inheritance refers to the ability of sub-forms (classes) to use the appearance of base Forms (classes) and implement code. When considering inheritance, note that the relationship between the two classes should be "Belong. For example, if the Employee is a Person and the Manager is a Person, both classes can inherit the Person class. However, the Leg class cannot inherit the Person class, because the Leg is not a Person. An abstract Class only defines the general attributes and methods that will be created by the subclass. When creating an abstract Class, use the keyword Interface instead of Class. The OO development paradigm is roughly: dividing objects → abstract classes → organizing classes into hierarchical structures (Inheritance and synthesis) → designing and implementing classes and instances. PolymorphismPolymorphisn is a technology 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. Overwrite 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 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, overloading is irrelevant 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, such a function address is bound at runtime (later bound ). The conclusion is that overload is only a language feature and is irrelevant to polymorphism and object orientation! Reference Bruce Eckel: "Don't be stupid. If it's not late, it's 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 of code reuse. Polymorphism aims 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. ConceptsIn general, the hollow triangle represents the inheritance relationship (class inheritance). In UML terminology, this relationship is called Generalization ). Person is a base class, and Teacher, Student, and Guest are sub-classes. Logically, if B is A "type" of A, and all functions and attributes of A are meaningful to B, B is allowed to inherit the functions and attributes of. For example, a Teacher is a Person and a Teacher is a kind of Person ). Then the class Teacher can be derived from the class Person (inherited ). If A is A base class and B is A derived class of A, B inherits data and functions of. If Class A and Class B are irrelevant, B cannot inherit the functions and attributes of Class A to make B more functional. Logically, if B is A kind of a, B is allowed to inherit the functions and attributes of. Aggregation (combination) Chart 2 combination if logically A is a part of B, B is not allowed to be generated from, instead, use A and other things to combine B. For example, Eye, Nose, Mouth, and Ear are part of the Head, therefore, the class Head should be composed of the class Eye, Nose, Mouth, and Ear, not derived (inherited. The types of aggregation include none, sharing (aggregation), and composite (combination. Aggregation Chart 3 shares the above diagram. There is a diamond (hollow) representing aggregation (the aggregation type is shared), and the meaning of aggregation indicates the has-a relationship. Aggregation is A relatively loose relationship. Aggregation Class B does not need to be responsible for the aggregated Class. Composition Chart 4 composite the only difference between this image and the above is that the diamond is solid, which represents a more robust relationship-composition) (The aggregation type is composite ). The link expressed by the combination is also has-a, but here, the life cycle of A is controlled by B. That is, A will be created with the creation of B and die with the demise of B. Dependency Chart 5 Dependency
Here, the relationship between B and A is only A Dependency relationship, which indicates that if Class A is modified, Class B will be affected.

This article is transferred:
Http://www.cnblogs.com/zxjyuan/archive/2009/12/11/1621621.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.