C # Virtual methods

Source: Internet
Author: User

If an instance method declaration is preceded with the virtual keyword, then this method is a virtual method.
The biggest difference between virtual and non-virtual methods is that the implementation of a virtual method can be replaced by a derived class , which is implemented by means of rewriting (later)
Characteristics of Virtual methods:
Static,abstract not allowed before virtual method, or override modifier
A virtual method cannot be private, so the private modifier cannot be used
Execution of virtual methods:
We know that the general function is statically compiled into the execution file at compile time, and its relative address is not changed during program operation.
While the virtual function is not statically compiled during compilation, its relative address is indeterminate, it will dynamically judge the function to be called according to the runtime object instance.
The class defined in the declaration is called the Declaration class, and the class that is instantiated at execution time is called an instance class.
such as: A A =new B (); Where a is a declaration class and B is an instance class.
1. When invoking a function of an object, the system will go directly to the object declaration definition of the class, that is, declare the class, see if the function called is a virtual function;
2. If it is not a virtual function, then it executes the function directly. And if it is a virtual function, then it does not immediately execute the function, but instead begins to examine the object's instance class.
3. In this instance class, he checks to see if there is a way to implement the virtual function or to re-implement the virtual function (via the override keyword) in the definition of the instance class.
If there is, it will not be looked up, but immediately execute the method of the virtual function implemented in the instance class. And if not, the system will keep looking up 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, and then execute the overloaded function in that parent class.
Example 1:

    Class A
{
public virtual void Sum ()
{
Console.WriteLine ("I am A class,i am virtual sum ().");
}
}
Class Program
{
static void Main (string[] args)
{
A a=new a (); Defines an object of this class A. This is a declaration of a class,
A.sum ();
Console.read ();
}
}

Execute a. Sum:
1. First check the declaration Class A 2. Check to be sum is virtual Method 3. Go to check instance Class A, the result is the problem itself
4. Perform the method of implementing sum in instance Class A 5. Output I am A class,i am virtual sum ().
Example 2:

Class A
{
public virtual void Sum ()
{
Console.WriteLine ("I am A class,i am virtual sum ().");
}
}
Class B:a
{
public override void Sum ()//re-implements virtual function
{
Console.WriteLine ("I am B class,i am override sum ().");
}

}
Class Program
{
static void Main (string[] args)
{
A a=new B (); Defines an object of this class A. This is a declaration class, instantiates a object, B is an instance class of a
A.sum ();
Console.read ();
}
}

Execute a. Sum:
1. First check the declaration Class A 2. Check to be virtual Method 3. Go to check instance Class B, there are overridden methods 4. Execute method in instance Class B 5. Output I am b class,i am override sum ().
Example 3:

    Class A
{
public virtual void Sum ()
{
Console.WriteLine ("I am A class,i am virtual sum ().");
}
}
Class B:a
{
public override void Sum ()//re-implements virtual function
{
Console.WriteLine ("I am B class,i am override sum ().");
}

}
Class C:b
{

}
Class Program
{
static void Main (string[] args)
{
A a=new C ();//Defines an object of this class A. This is a declaration class, instantiates a object, C is an instance class of a
A.sum ();
Console.read ();
}
}

Execute a. Sum:
1. Check the declaration Class A 2. Check to be virtual Method 3. Go to check instance Class C, no Override Method 4. Go to check Class C for the parent class B, there are overridden methods
5. Execute the sum method in the parent class B 6. Output I am B class,i am override sum ().
Example 4:

   Class A
{
public virtual void Sum ()
{
Console.WriteLine ("I am A class,i am virtual sum ().");
}
}
Class B:a
{
Public new void Sum ()//overrides the same name function in the parent class instead of re-implementing
{
Console.WriteLine ("I am B class,i am new sum ().");
}

}
Class Program
{
static void Main (string[] args)
{
A a=new B ();
A.sum ();
Console.read ();
}
}

Execute a. Sum:
1. Check the declaration Class A 2. Check to be virtual Method 3. Go to check instance Class B, no rewrite (this place to note, although B has implemented sum (), but does not use the Override keyword, so it is not considered to be rewritten) 4. Go to check the parent class A of Class B for itself 5. Executes the sum method in parent Class A 6. Output I am A class,i am virtual sum ().
So what if, in Example 4, the declaration is class B?

    Class A
{
public virtual void Sum ()
{
Console.WriteLine ("I am A class,i am virtual sum ().");
}
}
Class B:a
{
Public new void Sum ()//overrides the same name function in the parent class instead of re-implementing
{
Console.WriteLine ("I am B class,i am new sum ().");
}

}
Class Program
{
static void Main (string[] args)
{
b b=new B ();
B.sum ();
Console.read ();
}
}

Executes sum () in class B and outputs I am B class,i am new sum ().
Can I use abstract functions to override virtual functions in a base class?
The answer is yes.

    Class A
{
public virtual void Printfriends ()
{
Console.WriteLine ("A.printfriends ()");
}
}
Abstract class B:a
{
Public abstract override void Printfriends (); Use the override modifier to indicate that the abstraction overrides the implementation of the function in the base class
}
Abstract class C:a
{
public abstract new void Printfriends (); The modifier explicitly declaration, using the new modifier, means that the implementation of the function in the base class is hidden
}

Can a sealed class have virtual functions?
Yes, virtual functions in the base class are implicitly converted to non-virtual functions, but the sealed class itself cannot add new virtual functions

    Class A
{
Public virtual void Fun ()
{
Console.WriteLine ("I am A.");
}
}
Sealed class Program:a
{
public override void Fun ()
{
Console.WriteLine ("I am B.");
}
static void Main (string[] args)
{
Program P = new program ();
P.fun ();
Console.read ();
}
}
This article is copyright to the author and the blog Park is shared, welcome reprint, but without the author's consent must retain this paragraph, and in the article page obvious location in the form of hyperlinks to indicate the source, otherwise reserves the right to pursue legal responsibility.

Transferred from: http://www.cnblogs.com/jiajiayuan/archive/2011/09/14/2176015.html

C # Virtual methods

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.