This is the 21st day of my participation in the August More Text Challenge

Basic concept

Inheritance, encapsulation and polymorphism are the three main features of object-oriented programming.

Inherits new classes for creating reusable, extended, and modified behavior defined in other classes. Inheritance is the creation of a new class based on an existing class. An existing class is called a parent or base class, and a newly created class is called a subclass or derived class. The phenomenon of a subclass or derived class acquiring its properties from its parent class is called inheritance.

A derived class can have only one direct base class, that is, multiple inheritance is not supported. C# supports multi-level inheritance, which allows a derived class to be used as a new base class to generate a new derived class.

In general, base classes extract the common characteristics of derived classes, and a derived class changes an abstract base class into a concrete type by adding information. A derived class is a continuation of the definition of the base class, which is an embodiment of the base class.

All classes in C# inherit from the object class by default.


Derived class definition

The definition of a derived class is basically the same as that of a class, except after the class name: the base class name.

The definition form is:

Access modifierclassDerived class name: base class name {member list; }Copy the code

Classic example: Human and student

/// <summary>
///The base class
/// </summary>
class Person
{
    // Base class member -- name
    public string Name;
    // Base class member -- age
    public int Age;
}

/// <summary>
///Derived classes
/// </summary>
class Student : Person
{
    // Derived class member -- school
    public string School;
}
Copy the code

The members of a derived class are made up of two parts, one is inherited from the base class and the other is a new member defined by itself.

The member access attributes of derived classes can still be modified public, private, protected, internal, protected internal.


Derived class access

For members inherited from base classes, there are several rules:

  1. Members declared private in base classes can be inherited by derived classes, but cannot be accessed by derived class members and derived class users.
  2. Members declared protected in the base class can be accessed by derived class members, but not by derived class users.
  3. Members of a base class declared protected internal can be accessed by members of a derived class in the same program.
  4. Members of a base class declared as internal can be accessed by derived class members and derived class users in the same program.
  5. Members declared public in the base class are also not restricted in accessing derived classes.

Code examples:

class Program
{
    static void Main(string[] args)
    {
        Student st = new Student();
        
        // Access the public member inherited from the base class
        st.Name = "Czhenya";

        // Access a public member of a derived class
        st.School = "JunJin"; Console.ReadLine(); }}class Person
{
    // Base class member -- name
    public string Name;
    // Base class member -- age
    public int Age;
}

class Student : Person
{
    // Derived class member -- school
    public string School;
}
Copy the code

Constructors and destructors for derived classes

Derived classes do not inherit the constructors and destructors of the base class.

If you want to initialize a member of a derived class, you need to write the constructor of the derived class. If you want to release a derived object, you also need to call its own destructor.

  1. Constructor of a derived class

    • When designing constructors for derived classes, you need to consider initialization of new members of the derived class and data members inherited from the base class.

    • By default, when an instance of a derived class is created, the no-argument constructor of the base class is called.

A derived class can be set to use the specified base class constructor, the constructor of the derived class

The definition is as follows:

publicDerived class name (parameter list) :base(Argument list of the base class constructor to call) {function body}Copy the code

When creating an object of a derived class, the constructor of the base class is called first, and then the constructor of the derived class is called.

  1. Destructors for derived classes
    • The destructor defined in the derived class is used to clean up the newly added resources of the derived class. The destructor of the base class is used to clean up the resources requested by the base class.

    • The system automatically calls the destructors of derived classes and base classes to clean up objects.

When a derived object is destroyed, the destructor of the most derived class is called first, and then the destructor of the middle base class is called in descending order of backup until the destructor of the base class with the highest backup is executed.