C # Abstract method and virtual method understanding

Source: Internet
Author: User

Http://www.cnblogs.com/denylau/archive/2010/04/11/1709573.html

Http://www.cnblogs.com/naariah/archive/2011/07/13/2105255.html

Http://www.cnblogs.com/kdalan/archive/2012/05/29/2523470.html

Http://www.cnblogs.com/lykbk/archive/2012/06/01/lyk19900723_daima3.html

Http://www.cnblogs.com/qq4004229/archive/2012/10/23/2735477.html

First, the definition of virtual methods in C #:

public virtual void Virtualmethod () {...};

As long as you add virtual to the method name, it declares that the method is a virtual method, and that you cannot add the Abstract,static keyword declaration after declaring it as a virtual method.

The following is an example of inserting an instance to illustrate the difference between a virtual method and a non-virtual method

public class A
{
public virtual void Virmethod ()
{
Console.WriteLine ("Virtual Method A");
}
public void Normalmethod ()
{
Console.WriteLine ("Normal Method A");
}

}
public class B:a
{
public override void Virmethod ()
{

Console.WriteLine ("Virtual Method B");
}
Public new void Normalmethod ()
{
Console.WriteLine ("Normal Method B");
}
}

Execute in the main function:

b b = new B ();
A = b;//assigns an instance of a derived class to an object of the base class

A.virmethod ();
A.normalmethod ();
B.virmethod ();
B.normalmethod ();
Console.readkey ();

Execution Result: Virtual Method B

Normal Method A

Virtual Method B

Normal Method B

The result indicates that the A.virmethod () virtual method is an overriding method of Class B;

A.normalmethod (); A non-virtual method or a method that executes the class A itself;

A = b This step is to construct an object of the base class by an instance of the derived class, which is actually an object that points to a derived class; Virmethod () This step calls the method overridden by the derived class.

and A. Normalmethod () or method that calls the base class

(Insert here: Override is a virtual method that overrides the base class, new is the newly created method, which is the same name as the base class, but it is the own method of the subclass)

Override is overloaded (some translations are overwritten) and new is hidden.

Add: a A = B; Where a is the declaration class and B is the instance class

When calling a function, the system is going to look at the Declaration class first, if the function is not a virtual function, call the declaration class directly, if it is a virtual function, it will go to check the object's instance class. (personally think it makes sense)

If inheritance is the cornerstone of object-oriented design theory, then abstract theory and method is the pillar of inheritance theory. In the Chinese dictionary, the abstract is explained by the action or process of extracting or isolating the common characteristics or images of several distinct objects. It can be seen that abstraction is the extraction of features and properties from things, not the things themselves. How should we understand it?

As we all know, students have a common characteristic is to learn, then learn what, how to learn, every student is not the same. This can be extracted to learn this action as an abstract method, regardless of the specific content. Animals have called the action, the specific animal how to call, temporarily do not consider, first to extract the generality. Simply put, the class used to describe these commonalities is abstract, and abstract classes do not consider concrete implementations, only the behaviors that must be defined, are abstract methods.

An abstract method is a method that is not implemented and uses the keyword abstract to define an abstraction method. The syntax is as follows:

< access modifiers > Abstract return type method ();

For example:
public abstract void Cry ();

When an instance method declaration contains an abstract modifier, it is called an abstract methods. Abstract methods are only allowed to be declared in an abstract class, but cannot be implemented, and abstract method declarations contain only the signature of the method, which is overridden in its derived class using the Override keyword. In an abstract method declaration, any modifier in static, virtual, or override is not allowed.

Note: Unlike the normal method, the abstract method does not have closing curly braces, and adds ";" directly after the method brackets. Indicates that there is no method body in the abstract method, that is, there is no specific implementation.

C # Abstract method and virtual method understanding

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.