0x1 Introduction
Bringing up object-oriented, everyone has different views. But the most mentioned is: object, encapsulation, inheritance, polymorphism. Almost all of these elements form the basic logic of object-oriented design development. Object-oriented programming, what does "object" mean? The "object" here does not refer to "opposite-sex objects", but rather to what we often use in development: classes.
0X2 Object
Born: The object is like an individual person, born to enter the WTO, die and death. First, let's look at how an object is produced. After the new operation is performed, the system first allocates a certain amount of memory space in memory, initializes its additional members, and invokes the constructor to perform the initialization, so that a function completes the initialization.
Person Person=new man ("Big strong brother", 25);
Journey: To some extent, the process of changing object state information is achieved by interacting with objects through methods. This is also coincidence the way people live.
Person. Changeage (26)
Permissions: We all know that inheritance, the son can inherit the father's surname, genes, property and everything can be left behind, but does not mean that can inherit all. Because the father has some part of the privacy of the father unique, not inherit. Therefore, we need to identify in the program which inheritable or non-inheritable (where inheritance can be understood as access rights). The NET Framework has five access rights built into us:
- Public: The highest access rights , the company's board of directors have the highest decision-making power and management rights;
- Protected: Department manager, has the direct jurisdiction to this department, the object-oriented embodiment is the subclass inherits the vertical relation the Access Convention;
- Internal: Similar to the responsibilities of the company's functional departments, regardless of whether or not have a subordinate relationship, the Human resources department has the ability to govern the staff of other departments; (Object-oriented access to the same assembly, as long as it belongs to the same assembly, the object can access its properties, etc.). Whether or not there is affiliation;)
- Protected internal: Deputy General Manager, from the horizontal to the longitudinal has the management authority;
- Private: The lowest access rights , the company's general staff, well-managed themselves can;
0X3 inherit what is inheritance?
Figure 1 Inheritance Diagram
Inheritance is a kind of relationship between class and class. The inherited class becomes a subclass (in Figure 1 the car and the aircraft become subclasses), derived classes. The integrated class becomes the parent class, the base class, or the superclass. Integration enables subclasses to have properties and methods that are allowed to inherit from the parent class. Subclasses can also add new methods or properties at the same time to create a new class hierarchy.
The realization and nature of inheritance?
Figure 2 Implementation of inheritance
In the inheritance system shown in 2, vehicle is the base class for all modes of transport, which defines the "start" and "stop" methods that are available for all modes of transport. The Aricraft class implements the Iflyable interface, allowing Aricraft to implement the Fly () feature. This benefit is obvious, through the Iflyable interface, to achieve the separation of the object and behavior, so that we do not have to worry about improper inheritance and lead to the car also has the ability to fly (), to protect the system's robustness.
Public classVehicle { Public Virtual voidStart () {Console.WriteLine ("Vehicle is Start"); } Public Virtual voidStop () {Console.WriteLine ("Vehicle is Stop"); } } Public classcar:vehicle { Public Override voidStart () {Console.WriteLine ("Car is Start"); } Public Override voidStop () {Console.WriteLine ("Car is Stop"); } } Public classaircralt:vehicle, iflyable { Public voidFly () {Console.WriteLine ("Aircralt is Fly"); } } Public Interfaceiflyable {voidFly (); }Static voidMain (string[] args) {Vehicle car=NewCar (); Car. Start (); Console.ReadLine (); }Code Demo
Let's briefly analyze the object creation process:
Vehicle car = new car ();
Vehicle car is a reference that creates a vihicle type, and new car () creates a car object, allocates memory space and initialization, and assigns the object's reference to the car variable, which is the association of the car variable and the car object. We analyze how the CLR executes the object creation process at runtime from an inheritance perspective. We take car as an example, once the car object is established, it first uses recursion to find its parent class and allocates storage space for the parent class. The creation of an object is done in order to complete the memory creation of the entire parent class and its own fields. and the order in which the fields are stored is arranged from top to bottom, with the highest level of the row. The reason for this is that if the parent class and child class have properties with the same name, the editor automatically considers this to be two different fields to differentiate. Then there is the creation of the method table, and it is important to note that the creation of the party post is completed the first time the class is loaded into the AppDomain. When the object is created, it is only the typehandle of the object that refers to the loader heap that is published, that is, the method table exists prior to object creation. Similar to the process of creating a field, the method table is created as a subclass of the late father class. When a subclass method table is created, it is recursively down from its highest-level parent class. First copy all the virtual methods of the parent class, then compare it with the subclass method table, and overwrite the parent class's method with the subclass method if there is a rewrite operation. Also add a non-virtual method of the parent class to complete the creation of the car method table.
From our analysis and the process of creating objects above, we should have the following clearer understanding of inheritance:
- Inheritance is transitive, subclasses are extensions to the parent class, the parent class method must be inherited, and new methods can be added.
- Subclasses can call methods and fields of the parent class, but! The parent class cannot call the methods and fields of the child class.
- Subclasses inherit not only the common members of the parent class, but also the private members of the parent class, except those that are not accessible in the subclass. (I don't know if I can access it by reflection ....) I hope we can discuss this problem together. )
The classification and rules of inheritance?
Inherited classifications can be divided into two types: implementation inheritance and interface inheritance
To implement the difference between inheritance and interface inheritance:
Abstract classes are suitable for inter-class relationships with a family layer concept , and interfaces are best suited to provide common functionality for different classes
The interface focuses on the can-do relationship type, while the abstract class is biased towards the is-a -type relationship.
Interface multi-definition object behavior; Abstract class multi-definition object properties
Version-based problems are best implemented with abstract classes
Because value types are sealed, only interfaces can be implemented, and classes cannot be inherited
It's too late to work tomorrow,,, do not write, not to be continued ... People think that if you can, just a praise ....
【. NET Foundation supplements "in a comprehensible object-oriented