C # virtual method. abstract Method

Source: Internet
Author: User

 

First, define the virtual method in C:

Public Virtual void virtualmethod (){......};

If virtual is added before the method name, the method is declared as a virtual method. Note that abstract and static keywords cannot be added after the virtual method is declared.

Insert an instance to demonstrate the differences between the virtual method and the non-virtual method.

Public Class
{
Public Virtual void virmethod ()
{
Console. writeline ("virtual method ");
}
Public void normalmethod ()
{
Console. writeline ("normal method ");
}

}
Public Class B:
{
Public override void virmethod ()
{

Console. writeline ("virtual method B ");
}
Public new void normalmethod ()
{
Console. writeline ("normal method B ");
}
}

 

Execute the following in the main function:

           B = new B ();
           A A = B; // assign the instance of the derived class to the object of the base class

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

Execution result: Virtual Method B

         Normal method

          Virtual Method B

         Normal method B

The result indicates that the. virmethod () virtual method executes the rewrite method of Class B;

            A. normalmethod (); the non-virtual method still executes the method of Class A itself;

A A = B this step uses the instance of the derived class to construct the base class object. The base class pointer actually points to the object of the derived class. Therefore,. virmethod () calls the method rewritten by the derived class.

A. normalmethod () still calls the method of the base class.

(In this example, override overwrites the virtual method of the base class. New creates a new method, which is the same name as the base class, but it is still the method of the subclass)

Override is an overload (some translate to overwrite), while new is hidden.

Add: A = B, where A is a declaration class and B is an instance class.

When calling a function, the system first looks at the Declaration class. If the function is not a virtual function, it directly calls the Declaration class. If it is a virtual function, it will go to the instance class of the check object. (I think it makes sense)

 

If inheritance is the cornerstone of object-oriented design theory, abstract theories and methods are the pillars of the Inheritance theory. Abstract In a Chinese Dictionary refers to the action or process of extracting or isolating the common characteristics or images of several different objects. It can be seen that abstraction is to extract features and properties from things, not the things themselves. How should we understand it?

As we all know, a common feature of students is that they must learn. What to learn and how to learn are different for each student. In this way, we can extract and learn this action as an abstract method without considering the specific content. Animals are called actions. For the moment, we do not consider how specific animals are called. First, we extract commonalities. To put it simply, the class used to describe these commonalities is an abstract class. The abstract class only determines the behaviors that must be possessed without considering specific implementations. It is an abstract method.

Abstract methods are not implemented. abstract methods are defined using keywords abstract. Syntax:

<Access modifier> abstract return type method ();

For example:
Public abstract void cry ();

When an instance method Declaration contains an abstract modifier, it is called an abstract method ). Abstract METHODS can only be declared in abstract classes, but cannot be implemented. abstract method declarations only contain the signature of methods. abstract methods are rewritten using the override keyword in Their Derived classes. In the abstract method declaration, any modifier in static, virtual, or override is not allowed.

Note: Unlike the common method, the abstract method does not close the braces, and ";" is directly added after the method brackets, indicating that there is no method body in the abstract method, that is, there is no specific implementation.

 

Related Article

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.