In-depth analysis of C # inheritance mechanism 1

xiaoxiao2021-03-06  57

I. Inherit Basics

In order to improve the reusability and expandability of the software module, we always want to use the former or his previous development results, and hope to have enough flexibility during his development process. Sexual, unscrupulous module. C # This full-faced object design language provides two important features - inheritance inheritance and polymorphism Polymorphism.

Inheritance is one of the main features of object-oriented programming, which allows you to reuse code and save time. Inheritance is a intersection relationship between classes, enabling an instance of newly defined derived classes to inherit the features and capabilities of existing base classes, and new features or new features or new features that have been modified. level.

Many entities in the real world are not isolated from each other, and they tend to have a common differential. People can use hierarchies to describe the similarities and differences between these entities.

Figure 1

The above figure reflects the derived relationship of the vehicle class. The highest layer entity often has the most general, the more specific characteristics, the more specific, and the lower layer contains the characteristics of the upper layer. The relationship between them is the relationship between the base class and the derived class.

In order to model the hierarchy of the real world in the software language, the object-oriented programming technology introduced the concept of inheritance. When a class is derived from another class, the derived class inherits the characteristics from the base class. Detective class can also be used as a base class for other classes. The multilayer class derived from a base class forms a class hierarchy.

Note: In C #, derived classes can only be inherited from one class. This is because in C , people don't need a class from multiple classes in most cases. From multiple base classes, a class is often brought about many problems, thereby offsetting the advantages of this flexibility.

C #, derived from its direct base class inherited members: method, domain, attribute, event, index indicator. In addition to constructor and destructor, derived class implicitly inherits all members of the direct base class. Look into the example below:

Using System; Class Vehicle / / Defines Transportation (Car) {Protected Int Wheels; // Public Member: Wheel Number Protected Float Weight; // Protection Member: Weight PUBLIC VEHICLE () {;} Public Vehicle (int W, Float g) {Wheels = W; Weight = g;} public void speak () {console.writeline ("The number of wheels of the vehicle can vary!");}}; class car: vehicle // Define Cars : Inheriting {Int passengers; // Private member: Public Car (int W, Float G, INT P): Base (w, g) {wheels = W;} = g; passengers = p;} }

Vehicle as a base class, embodies the public nature of the "car": the car has a wheel and weight. The CAR class inherits these properties of Vehicle and adds its own characteristics: You can carry passengers.

转载请注明原文地址:https://www.9cbs.com/read-83909.html

New Post(0)