当前位置:网站首页>C # advanced learning -- virtual method

C # advanced learning -- virtual method

2022-06-23 08:30:00 Ambassador Tengyun

One . Definition

To put it simply , Virtual methods are methods that can be overridden by subclasses , If the subclass overrides the virtual method , Then the runtime will use the rewritten logic , If you don't rewrite it , Then use the logic of the virtual method in the parent class .

Define keywords :virtual

Rewrite keyword :override

Two . example

Define a Person Class and Person Class Student Subclass , as follows :

 public class Person
    {
        public string  Name { get; set; }

        public virtual int Age { get; set; }

        public virtual void SayHello()
        {
            Console.WriteLine(" Hello everyone , I am a "+this.Name+"  Age "+this.Age);
        }
        public virtual int Add(int a)
        {
            return this.Age+a;
        }
    }

Suppose that the current student class has the following needs :

1. Need to be in SayHello Method to add the return of student ID
2. Age needs to be checked , When you are older than 18 At the age of , Go straight back to 18
  public class Student:Person
    {
        public string StuNO { get; set; }

        public override int Age
        {
            get => base.Age;            
            set => base.Age = value>18?18:value;
        }
        public override void SayHello()
        {
            Console.WriteLine(" Hello everyone , I am a " + this.Name+", Age "+this.Age+", My student number is "+this.StuNO);
        }

    }

We see , For demand 1 and 2, Rewrote Age Properties and SayHello Method , Call... On the console SayHello Method , The method to invoke is as follows :

 Person person = new Person();
 person.Name = " people ";
 person.Age = 25;
 person.SayHello(); 

 Student student = new Student();
 student.Name = " Student ";
 student.Age = 25;// The age here is older than 18
 student.StuNO = "001";
 student.SayHello();

The results are as follows :

This is the time , We are Student Class Add Method ( It's not rewriting ), Then override the... In the parent class Add Method , as follows :

  public int Add(int a,int b) // This is a new method , Indicates that the class with the same name in the parent class is overwritten , Instead of re implementing 
        {
            return this.Age + a + 10;
        }

        public override int Add(int a)  // This is in the overridden parent class Add Method 
        {
            return base.Add(a)+666;
        }

At this time , Call... On the console Add When the method is used , If two parameters are used , Then execute the new method , If you take a parameter , Execute the override method .

If you use the method of subclass instantiating the parent class , as follows :

Person aaa = new Student();
aaa.SayHello();

Pass the verification , We will find that , At this moment SayHello The execution is overridden in a subclass SayHello Method

On the implementation order of virtual methods of subclasses and superclasses , Here is the summary of the predecessors :

1、 When calling a function of an object , The system will directly check the class defined by the object declaration , Declaration class , See if the called function is a virtual function ;

2、 If it's not a virtual function , Then it executes the function directly . And if there is virtual keyword , That is, an imaginary function , Then it won't execute the function immediately , Instead, check the instance class of the object .

3、 In this instance class , He will check whether the virtual function is re implemented in the definition of the instance class ( adopt override keyword ), If there is , that OK, It won't look for it anymore , And immediately execute the re implemented function in the instance class . And if not , The system will keep looking up for the parent class of the instance class , And repeat the check in the instance class for the parent class , Until you find the first parent class that overloads the virtual function , Then execute the overloaded function in the parent class .

thus , Conclusion of virtual method ......

Last , In fact, all data tests can be Cloud server Conduct , You can see that Tencent cloud Services related to , The server you bought as test data is very good

原网站

版权声明
本文为[Ambassador Tengyun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/01/202201112147583800.html