I have been diving in the blog Park for a few years. I have read a lot but I have little to remember. Several yearsProgramAfter the employee's work experience, he began to find that there are indeed a lot of projects, but his own things are almost nothing. Just taking advantage of the company's free time, I bought a few books to read about it. It was just a warm story, and I also had a good look at some basic knowledge that I didn't pay much attention to before. I also hope to provide some help to some new users.
The basic knowledge about the abstract class in inheritance that needs to be recorded today
-
- Abstract classes and Abstract Functions
Abstract classes are defined as abstract keywords. abstract classes cannot be instantiated, and abstract functions are not instantiated. They must be rewritten in non-Abstract Derived classes. Abstract Functions are virtual and do not need to be modified by virtual. If they are used, an error is generated. If a class contains an abstract method, the class must be declared abstract.
-
- Sealing and sealing methods
Declare the class and method as sealed and express it as sealed. The sealed class cannot be inherited, And the sealed method cannot be overwritten.
In general, it is most likely to declare sealed: If you want to perform operations on classes, classes, or other classes written by yourself on classes or methods outside the domain, rewriting certain functions will leadCodeChaos. It can also be marked as sealed by commercial crude oil bars or methods to prevent third-party extension in the form of illegal authorization protocols.
- Derived classes and constructors
The execution of the constructor is performed from top to bottom according to the hierarchy until the class to be instantiated by the compiler is reached. In this process, each constructor initializes fields in its own class. Therefore, we try to follow this principle when adding our own constructor.
The execution sequence of the constructor. The constructors in the base class are always called first. That is to say, if the constructor of a derived class can call any base class method, attribute, and any other member that he can access during execution, because the base class has been constructed, other fields are also initialized.
Public Abstract Class Mybaseabsclass
{
Private String Name;
Public Mybaseabsclass (){}
PublicMybaseabsclass (StringName)
{
Console. writeline (1);
This. Name=Name;
}
}
Public Class Subclass1: mybaseabsclass
{
Private String Referrername;
Subclass1 (){}
PublicSubclass1 (StringName)
:Base(Name)
{
Console. writeline (3);
}
Public Subclass1 ( String Name, String Referrername)
: Base (Name)
{
Console. writeline ( 2 );
This . Referrername = Referrername;
}
}