How to use virtual functions in c #

Source: Internet
Author: User

If an instance method has a virtual keyword before it is declared, this method is a virtual method.

The biggest difference between a virtual method and a non-virtual method is that the implementation of a virtual method can be replaced by a derived class, which is implemented through method rewriting (I will discuss it later)
Features of Virtual Methods:
Static, abstract, or override modifiers are not allowed before virtual methods.
Virtual methods cannot be private, so private modifiers cannot be used.
Execution of Virtual Methods:
We know that the general function is statically compiled into the execution file during compilation, and its relative address remains unchanged during the program running,
While a virtual function is not statically compiled during compilation, its relative address is uncertain. It dynamically judges the function to be called Based on the object instance during the runtime,
The class defined during the declaration is called the Declaration class, and the class instantiated during execution is called the instance class.
For example, A a = new B (), where A is the declarative class, and B is the instance class.
1. When a function of an object is called, the system will directly check the declared class of the object to see whether the called function is a virtual function;
2. If it is not a virtual function, it will directly execute the function. If it is a virtual function, it will not immediately execute the function at this time, but will begin to check the instance class of the object.
3. In this instance class, he will check whether there are methods in the definition of this instance class to implement this virtual function or re-implement this virtual function (by using the override keyword,
If there is, it will not find again, but will immediately execute the virtual function method implemented in the instance class. If no, the system will keep searching for the parent class of the Instance class,
Repeat the previous check in the instance class for the parent class until the first parent class with the virtual function reloaded is found, and then the overloaded function in the parent class is executed.
Example 1:

Copy codeThe Code is as follows: class
{
Publicvirtualvoid Sum ()
{
Console. WriteLine ("I am A Class, I am virtual sum ().");
}
}
Class Program
{
Staticvoid Main (string [] args)
{
A a = new A (); // defines an object of Class a. A is the declarative class of Class A. it instantiates an object and a is the instance class of Class.
A. Sum ();
Console. Read ();
}
}

Run a. Sum:
1. Check the declarative Class A. 2. Check that sum is A virtual method. 3. Check instance Class A. The result is the question.
4. Execute the Sum method in instance Class A. 5. output result I am A Class, I am virtual sum ().
Example 2:

Copy codeThe Code is as follows: class
{
Publicvirtualvoid Sum ()
{
Console. WriteLine ("I am A Class, I am virtual sum ().");
}
}
Class B:
{
Publicoverrisponid Sum () // implements the virtual function again.
{
Console. WriteLine ("I am B Class, I am override sum ().");
}

}
Class Program
{
Staticvoid Main (string [] args)
{
A a = new B (); // defines an object of Class a. A is the declarative class of Class A. it instantiates an object and B is the instance class of Class.
A. Sum ();
Console. Read ();
}
}

Run a. Sum:
1. first check the declarative Class A 2. check to be a virtual method 3. to check instance Class B, rewrite the method 4. execute the method in instance Class B 5. output result I am B Class, I am override sum ().
Example 3:Copy codeThe Code is as follows: class
{
Publicvirtualvoid Sum ()
{
Console. WriteLine ("I am A Class, I am virtual sum ().");
}
}
Class B:
{
Publicoverrisponid Sum () // implements the virtual function again.
{
Console. WriteLine ("I am B Class, I am override sum ().");
}

}
Class C: B
{

}
Class Program
{
Staticvoid Main (string [] args)
{
A a = new C (); // defines an object of Class a. A is the declarative class of Class A. it instantiates an object and C is the instance class of Class.
A. Sum ();
Console. Read ();
}
}

Run a. Sum:
1. first check the declarative Class A 2. check to be a virtual method 3. switch to check instance Class C, no rewrite Method 4. to check the parent class B of class C, there is a rewrite method.
5. Execute the Sum method in parent Class B 6. output the result I am B Class, I am override sum ().
Example 4:

Copy codeThe Code is as follows: class
{
Publicvirtualvoid Sum ()
{
Console. WriteLine ("I am A Class, I am virtual sum ().");
}
}
Class B:
{
Publicnewvoid Sum () // overwrite the Same Name function in the parent class, instead of re-Implementing
{
Console. WriteLine ("I am B Class, I am new sum ().");
}

}
Class Program
{
Staticvoid Main (string [] args)
{
A a = new B ();
A. Sum ();
Console. Read ();
}
}

Run a. Sum:
1. first check the declarative Class A 2. check to be a virtual method 3. check instance Class B for forwarding without rewriting (note that although Sum () is implemented in B, the override keyword is not used, so it is not considered as a rewrite) 4. to check the parent class A of Class B, it is itself 5. execute the Sum Method 6 in parent Class. output result I am A Class, I am virtual sum ().
So what if Class B is declared in Example 4?Copy codeThe Code is as follows: class
{
Publicvirtualvoid Sum ()
{
Console. WriteLine ("I am A Class, I am virtual sum ().");
}
}
Class B:
{
Publicnewvoid Sum () // overwrite the Same Name function in the parent class, instead of re-Implementing
{
Console. WriteLine ("I am B Class, I am new sum ().");
}

}
Class Program
{
Staticvoid Main (string [] args)
{
B B = new B ();
B. Sum ();
Console. Read ();
}
}

Execute Sum () in Class B and output the result I am B Class, I am new sum ().
Can abstract functions be used to override virtual functions in the base class?
The answer is yes.

Copy codeThe Code is as follows: class
{
Publicvirtualvoid PrintFriends ()
{
Console. WriteLine ("A. PrintFriends ()");
}
}
Abstractclass B:
{
Public abstract override void PrintFriends (); // use the override modifier to overwrite the implementation of this function in the base class.
}
Abstract class C:
{
Public abstract new void PrintFriends (); // explicitly declare with the new modifier to hide the implementation of this function in the base class
}

Can there be virtual functions in the seal class?
Yes. The virtual functions in the base class are implicitly converted to non-virtual functions, but the new virtual functions cannot be added to the sealed class itself.

Copy codeThe Code is as follows: class
{
Publicvirtualvoid Fun ()
{
Console. WriteLine ("I am .");
}
}
Sealedclass Program:
{
Public override void Fun ()
{
Console. WriteLine ("I am B .");
}
Staticvoid Main (string [] args)
{
Program p = new Program ();
P. Fun ();
Console. Read ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.