[C# Study Notes (1)] Detailed explanation of the override keyword (reprinted)

Preface

There are concepts of overriding, overloading, and overwriting in C#, and the corresponding keywords are override, overload, and new. This article mainly introduces the override keyword, and mainly refers to this article C# Rewriting (override ) .

text

1. Introduction to rewriting, overloading and overwriting

1. Override

Override: The subclass rewrites the method body in the parent class according to its own needs. The access permissions, return values, method names, and parameters of the overridden method and the parent class method remain unchanged.

2. Overload

Overload: Writing multiple methods with the same method name in a class according to different requirements. The method names are the same, but the type or number of parameters cannot be the same, and the return value type cannot be used as a sign of overloading.

3. Overwrite (new)

Overwrite: implemented using new. Using the new keyword in a subclass to modify a defined method with the same name as in the parent class is also called overwriting. Overriding will not change the function of the parent class method.

2. Specific examples of rewriting

using System;

namespace ConsoleApp
{
    
    
    class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            A a = new B();
            a.Print();
        }
    }

    class A
    {
    
    
        public A()
        {
    
    
            //构造函数
            Print();
        }
        public virtual void Print()
        {
    
    
            Console.WriteLine("我是A类");
        }
    }

    class B:A
    {
    
    
        public B()
        {
    
    
            //构造函数
            Print();
        }
        public override void Print()
        {
    
    
            Console.WriteLine("我是B类"); //重写Print()方法
        }
    }
}


In the above code, class A is the parent class, class B is the subclass, and the Print() method in class A is overridden.

3. Conditions for overriding parent class methods

  1. Methods in the parent class must be modified with the virtual keyword before they can be overridden by the subclass. If the parent class method does not use the virtual keyword, a compilation error will occur when overriding the parent class method in the subclass;
  2. Virtual methods: Methods defined in the base class that are allowed to be overridden in derived classes are defined using the virtual keyword;
  3. When rewriting the parent class in a subclass, you need to use the override keyword as an identifier;
  4. For example, when class B inherits class A and overrides the Print() method in class A, the following warning will appear if the override keyword is not used.
    Insert image description here

4. After the parent class method is overridden by the subclass, when the subclass object is created to point to the parent class, when the overridden method is called, is the method of the parent class or the subclass called?

When a parent class method is overridden by a subclass, as long as an instance of the subclass is created, when the overridden method is called, the methods in the subclass will eventually be run.

The result of running the code:
Insert image description here

  1. When creating an instance of a subclass, the constructor in the subclass is executed first, but the constructor in the parent class is executed first;
  2. When executing the Print() method in the constructor of the parent class, first determine whether the Print() method has been overridden. If the method has been overridden, the overridden method in the subclass will be executed;
  3. After the constructor of the parent class is executed, the constructor of the subclass will be executed. When the subclass constructor calls the Print() function, it will first determine whether the subclass contains the Print() method. If so, it will call its own Print() method, otherwise call the Print() method in the parent class;
  4. Finally, a.Print() also first determines whether the Print() method in class A has been overridden. If so, it calls the Print() method in class B.

5. When the overridden method in the subclass is modified by the new keyword instead of the override keyword, whether the method in the parent class is overridden

It will not be overridden. After being modified with the new keyword, the Print() method is not an overridden method, but a new method in class B.
Insert image description here
The running results in the above figure show that when the new keyword is used, as long as it is related to the parent class, the Print() method that is run will run the Print() method in the parent class.

Guess you like

Origin blog.csdn.net/sallyyellow/article/details/131719300