There is a lot of content in this section, and the video file exceeds 4M, so the downloading time may be a little longer. Please wait a little longer!
There is a lot of content in this section, because there is a lot of content in inheritance alone, so I will take the opportunity to I have used all the previous things, which is considered as a review. Some things in this section are explained over and over again two or three times. The purpose is to hope that everyone can master them. This is the most important thing! ! If you are not sure about the review content in this section, please read the previous relevant chapters! !
Next we talk about inheritance. To understand inheritance, we first need to know why inheritance is needed?
Suppose that a class we made for the news system already has database connection, initialization, and functions such as query, addition, deletion, modification, etc.!
Then we will definitely have to do some different queries, modifications, etc. For example, the news column and user management both need to use the classes we mentioned earlier, but the difference is that they require different results. what to do? There are two ways. One is to create classes for them separately and copy the database connection class twice. I think the disadvantages of this need not be mentioned. Then we can also frequently call members of the database class to achieve the goal. But if this happens, wouldn't our code be very bloated? Do you know why weight loss pills are so popular?
Depressed, what should I do? Simple, we use inheritance to complete it. Let's briefly describe inheritance. It is that the subclass (derived class) has all the characteristics of the parent class (base class), and at the same time has its own characteristics. I think this should make it clear!
Note: .Net inheritance only allows inheritance directly from another class, and cannot inherit multiple classes at the same time. It seems that there can only be one dad (actually this is not absolute, haha, it is really interesting to think about Microsoft, we can still Use interfaces to implement multiple inheritance), but multi-level inheritance is possible. For example, there can be a father and a grandfather at the top, and a son and grandson at the bottom.
Let’s take a look at something intuitive:
If we look at the picture above, we will find that the lower levels are more specific. Then we can understand that human is the base class (parent class) of all classes, and the following are derived classes (subclasses). In fact, our following subcategories can be extended downwards.
In C#, a derived class inherits members from its direct base class: methods, fields, properties, events, and index indicators. A derived class implicitly inherits all members of the direct base class except constructors and destructors.
2. Inheritance in C# complies with the following rules:
(This is not a summary by me, I went online and copied it from someone else. Haha!!)
1. Inheritance is transitive. If C is derived from B, and B is derived from A, then C not only inherits the members declared in B, but also inherits the members in A. The Object class serves as the base class for all classes.
2. Derived classes should be extensions of base classes. A derived class can add new members, but cannot remove the definitions of inherited members.
3. Constructors and destructors cannot be inherited. In addition, other members can be inherited regardless of the access methods defined for them. The way members are accessed in the base class can only determine whether derived classes can access them.
4. If a derived class defines a new member with the same name as an inherited member, it can overwrite the inherited member. But this does not mean that the derived class has deleted these members, it is just that these members can no longer be accessed. (It should be noted that if an attribute or method has the same name but a different signature, it is not an overwrite, but an overload.)
5. A class can define virtual methods, virtual attributes and virtual index indicators, and its derivation Classes can overload these members so that implementing classes can exhibit polymorphism.
6. Derived classes can only inherit from one class, and multiple inheritance can be achieved through interfaces.
In a subclass, we can access members of the base class through the base keyword:
call methods on the base class that have been overridden by other methods.
Specifies the base class constructor that should be called when creating an instance of a derived class.
Base class access is only possible in constructors, instance methods, or instance property accessors.
It is an error to use the base keyword from a static method.
Revisiting Access Modifiers
Access modifiers are keywords that specify the accessibility of a declared member or type. There are four access modifiers in class inheritance: public protected internal private. Use these access modifiers to specify the following five accessibility levels: public protected internal internal protected private.
Declared accessibility meaning
Public access is unrestricted.
protected access is limited to the containing class or types derived from the containing class.
Internal access is limited to the current project.
protected internal Access is limited to the current item or type derived from the containing class.
private access is limited to the containing type.
Let’s look at an example below, let’s stick to the topic of people.
See the comments for all the codes!!