Virtual function in C# virtual

Virtual function

A member function declared virtual in a base class and redefined in one or more derived classes is called a virtual function.

The role of virtual functions is to achieve polymorphism, which separates the interface from the implementation.

C# is a fully object-oriented language, all functions are not virtual by default, but the keyword virtual can be declared in the base class, and the function can be overridden by the keyword override in its derived class. The rewritten virtual function is still a virtual function. Since virtual is only meaningful for instance function members in the class, neither member fields nor static functions can be declared as virtual, nor can they be used with override and abstract.

The difference between virtual function and general function

Generally, the function is statically compiled into the executable file when compiling, and its relative address does not change during the running of the program, that is, hard-coded! The virtual function is not statically compiled during compilation, and its relative address is uncertain. It will dynamically determine the function to be called based on the object instance at runtime. The class defined at the time of declaration is called the declaration class, and the instance at execution time The modified class is called an instance class.

How to use virtual

1. When calling a function of an object, the system will directly check the class defined by the object declaration, that is, the declared class, to see if the called function is a virtual function.

2. If it is not a virtual function, then it executes the function directly. And if there is a virtual keyword, that is, a virtual function, then it will not immediately execute the function at this time, but will instead check the instance class of the object.

3. In this instance class, he will check whether there is a reimplementation of the virtual function (via the override keyword) in the definition of the instance class, and if so, immediately execute the reimplemented function in the instance class. If not, the system will continue to look up the parent class of the instance class, and repeat the check in the instance class for the parent class until it finds the first parent class that overloads the virtual function, and then executes the The overloaded function in the parent class.

E.g:

using System;
using System.Text.RegularExpressions;

namespace Test_Code
{
    class A
    {
        public virtual void Fun() //virtual关键字,表明这是一个虚函数
        {
            Console.WriteLine("Fun In A");
        }
    }

    class B : A  // 继承
    {
        public override void Fun() //override关键字,重新实现了虚函数
        {
            Console.WriteLine("Fun In B");
        }
    }

    class C : B //继承
    {

    }

    class D : A //继承
    {
        public new void Fun()
        {
            Console.WriteLine("Fun In D");
        }
    }

    class Program
    {
        static void Main()
        {
            A a = new A();  // A为声明类,A为实例类
            A b = new B();  // A为声明类,B为实例类
            A c = new C();  // A为声明类,C为实例类
            A d = new D();  // A为声明类,D为实例类
            a.Fun();  //执行a.Fun :1.先检查声明类A 2.检查到是虚方法 3.转去检查示例类A,就为本身
                      //           4.执行实例类A中的方法 5.输出 Fun In A
            b.Fun();  //执行b.Fun :1.先检查声明类A 2.检查到是虚方法 3.转去检查示例类B,有重载的
                      //           4.执行实例类B中的方法 5.输出 Fun In B
            c.Fun();  //执行c.Fun :1.先检查声明类A 2.检查到是虚方法 3.转去检查示例类C,无重载的
                      //           4.转去检查类C的父类B,有重载 5.执行父类B中的Fun方法 6.输出结果 Fun In B
            d.Fun();  //执行d.Fun :1.先检查声明类A 2.检查到是虚方法 3.转去检查示例类D,无重载的
                      //           4.转去检查类D的父类A,就为本身 5.执行父类A中的Fun方法6.输出结果 Fun In A

            D d1 = new D();
            d1.Fun(); // 执行D类的Fun(),输出结果 Fun In D
            Console.ReadLine();
        }
    }
}

Guess you like

Origin blog.csdn.net/qq_40513792/article/details/114286329