What is the role of Virtual Methods in the base class?

Source: Internet
Author: User
What is the role of Virtual Methods in the base class?

14:57:37 | category:

. Net/C # | Tag:
| Large font size, medium/small subscription

Only the methods of the base class can be override with the keyword virtual, so as to realize the most important feature of object-oriented-polymorphism, that is, the base class can use the method of the derived class.

C # states: normal method overloading: refers to the same name of two or more methods in the class (including hidden and inherited methods, as long as the parameter type or number of parameters are different, the compiler knows under which method should be called.

When a new virtual function is defined in a derived class, the required method name, return value type, number, type, and sequence of parameters in the parameter table must be exactly the same as the virtual function in the base class.

Simply put, the override method in the subclass can overwrite the accumulated virtual method. When you convert an instance of a subclass to the base class, the override method of the subclass is called when the method is called.

Two functions with the same name in a class are called "overload", and the functions with the same name in the base class and the inheritance class are called "Overwrite". virtual functions must be used for rewriting.

The base class method can be override by the derived class only after the key word virtual is added to the base class method, so as to realize the most important feature of object-oriented-polymorphism, that is, the base class can use the method of the derived class.

The methods in the parent class are duplicated, providing different real-time methods for a specific row (inserting different numbers );
It can be understood as a duplicate of the corresponding method of the parent class in the Child class (but it is not the same in actual scenarios) after the sub-classes are fully copied to the parent class, the replacement class of the override (override) is dug and used on the new replacement class of the sub-class. If the sub-class does not have a duplicate (override is not used) and the method is exactly the same as that of the parent class, it can be understood that the subcategories do not support the generation of tokens, but they are just a simple overwrite. When we want to use the token, we only need the type-sensitive parameter the replacement of the overwrite occurs.

For example:
Class {
Public Virtual void Ma (string S)
{Console. writeline ("Class A:" + S );}
Public Virtual void Ma (int I)
{Console. writeline ("Class A:" + I. tostring ());}
}

Class B: {
Public override void Ma (string s) // completely replaces the parent class
{Console. writeline ("Class B:" + S );}
Public new void Ma (int I) // override the parent class when writing
{Console. writeline ("Class B:" + I. tostring ());}
}

Void main (){
B = new B ();
A A = (B) B; // convert B -->
A. Ma ("Li ");
A. Ma (100 );}

Output:
Class B: Li
Class A: 100

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.