Three Characteristics of object-oriented
Three Characteristics of object-oriented
(This figure is from the Network)
1. encapsulation is one of the most important features of object-oriented systems. encapsulation refers to hiding. The object hides data (such as private attributes), avoiding excessive coupling and excessive dependency caused by direct access to other objects and using object attributes. At the same time, it can also prevent other objects from arbitrarily modifying the internal data of the object and cause inconsistency of the object. To access the mathematics of the object, you must use the related functions provided by the object. Implementation Details of object hiding methods. A) users can only use public methods but not those protected or private methods. You can modify these non-public methods without affecting users. B) You can hide specific types. Users can use them without having to concern themselves with the real types of objects (depending on interfaces or abstract bands ). C) the user does not need to relate to those classes irrelevant to the user, reducing coupling. Because they can only be used through public interfaces and methods, the client program will not be able to use those protected methods (such as methods modified with the private or protected keyword ), however, you can change these methods without affecting users. This reduces coupling.
Interface Display {public void display ();} class Person implements Display {private int age; // hidden protected String name; public Person (int age, String name) {this. age = age; this. name = name;} public int getAge () {return age;} public void setAge (int age) {this. age = age;} public void display () {System. out. println (age: + age +, name: + name );}}
A typical example of method hiding is the singleton mode.
2. inheritance is another important feature of object orientation. Inheritance can use different class objects with the same behavior. To use other class methods, we do not have to rewrite these old methods as long as this class (subclass) inherit the class (parent class) of the included methods. From the bottom up, inheritance can reuse the features of the parent class; from the top down, inheritance can extend the features of the parent class.
Class Student extends Person {// inherit private int ID; public Student (int age, String name, int ID) {super (age, name); this. ID = ID;} public void display () {System. out. println (age: + this. getAge () +, name: + this. name +, ID: + this. ID );}}
3. polymorphism allows us to process different types of objects in the same way. We can use a piece of code to process different types of objects as long as they inherit/implement the same type. In this way, we do not need to write the same logic for each type of object, greatly improving code reuse. In the above two classes, we do not need to know the specific object type. Through the interface, we can call the display () function to display the information of the corresponding object. Reference: the design model from the beginning of object-oriented Liu Jihua Tsinghua University Press reprinted please note the Source: http://blog.csdn.net/lvsaixia/article/details/41085555