Object-Oriented Programming learning notes, object-oriented learning notes
Object-Oriented Programming is different from process-oriented programming. The operation unit is class, while the process-oriented unit is method. That is, process-oriented programming is composed of one method after another. Object-Oriented Programming is composed of one class after another. Compared with process-oriented, object-oriented code is more reusable, code is more concealed, and more in line with human thinking.
The most basic object-oriented unit is class. A Class is an abstract concept that describes a certain type of things. For example, a cat, a dog, or a human. Objects are class instantiation. It is a specific single member that conforms to the class description. For example, I am an object instantiated by the Person class. I am a specific individual Member that meets the description. The process of object creation is called class instantiation.
Class contains the State (such as name, gender) and behavior (singing, dancing, and coding) that describes a thing ).
On the. net platform, the most basic programming structure is the class type. Formally speaking, a class is a custom type consisting of field data (usually called member variables) and members that operate on the data (such as constructors, attributes, methods, and events. Field data indicates the status.
Class Person {// indicates the String Name; String Sex of the field in its own State; // indicates the behavior method Public void Sing (); Public void Dance (); public void Coding ();}
Three features of Object-Oriented Programming (OOP:
1. Encapsulation
Encapsulation is a language capability that hides implementation details that are not required to be understood by object users (who program using classes you write.
1 // DatabaseReader encapsulates the details of Database Operations 2 DatabaseReader dbReader = new DatabaseReader (); 3 dbReader. Open (@ "C: \ AutoLot. mdb ");
The concept closely related to the encapsulation programming logic is the concept of data protection. Ideally, the State data of an object should be concealed, and external access is not allowed directly. The access function can only be used for "polite access ".
2. Inheritance
Inheritance refers to the ability to create a mental class based on an existing type. Through inheritance, subclasses can inherit the core functions of the base class (or parent class) and extend the behavior of the base class.
We can see that "rectangle is a graph, and a graph Is an object" is constructed into a "Is-a" relationship, which is also called an inheritance relationship.
We will think that Shape defines members (color, height) that are public to many derived classes ). Because Rectangle extends Shape, it inherits the core functions defined by Shape and Object. It also has its own unique features.
Unlike C ++, C # does not support the concept of multi-inheritance (so the concept of interface is introduced). A major advantage of inheritance is that it improves code reusability, in OOP, there is another form of code reuse: including/delegate model (has-a relationship, also aggregation). This form of reuse is not used to establish the parent class and subclass relationship. It means that a class can define the member variables of another class and indirectly expose its functions to the object users.
For example, to build a Car class, you want to express the concept of a has-a radio in a Car. Obviously, it is not logical to let the Car inherit the Radio class. Two classes are required to work together. The Car class creates and discloses the Radio function.
1 Class Car2 {3 Radio _Radio;4 }5 Class Radic6 {7 8 }
3. Polymorphism
The same operation acts on different objects and can have different interpretations to produce different execution results. This is polymorphism.
In fact, an instance of the same type calls the "same" method and the results are different. The "same" is enclosed in double quotes because the same method only looks the same, but actually they call different methods.
When both CirCle and Rectangle call Draw (), it seems that the two instances call the "same" method, but they actually call their respective Draw () methods. So it seems to be the same, but it is actually a different method. And different execution results are generated, which is polymorphism.
At the same time, the polymorphism mentioned here is also simple polymorphism. Polymorphism is used to specify some implementation methods for the base class, And the subclass only needs to override these methods. This enhances code reusability.