Three ways to abstract a class

Source: Internet
Author: User

There are three kinds of methods in the abstract class.

1. Abstract methods

2. Common methods

3. Virtual methods

1  Abstract classClass12     {3         //Abstract methods in abstract class Class14          Public Abstract voidstr1 ();5         //common methods in abstract class Class16          Public voidstr2 ()7         {8Console.WriteLine ("Common Methods");9         }Ten         //the virtual method inside the abstract class Class1 One          Public Virtual voidSTR3 () A         { -Console.WriteLine ("This is a virtual method"); -         } the  -}

So, what is the difference between the three methods?

1 classClass2:class12     {3         //overloads of the abstract method str14          Public Override voidstr1 ()5         {6Console.WriteLine ("overloading of abstract methods");7         }8         //inheritance of virtual method Str39          Public Override voidSTR3 ()Ten         { One             Base. STR3 (); AConsole.WriteLine ("I rewrote the Str3 method"); -         } -         //inheritance of common method str2 the          Public voidstr () -         { -             Base. STR2 (); -         } +}

Console:

1   class Program2     {3         Static voidMain (string[] args)4         {5Class2 JJ =NewClass2 ();6 jj.str1 ();7 JJ.STR3 ();8 jj.str ();9 Console.readkey ();Ten         } One  A}

Operation Result:

Summarize:

1. Abstract methods

Abstract methods cannot have method bodies, which can be overloaded after they are inherited. and must be inherited.

2. Common methods

Normal methods can be inherited or not inherited. The difference from virtual methods is that the normal method cannot be overloaded, that is, the method body cannot be changed.

3. Virtual methods

Virtual methods can be inherited or not inherited. The difference from the normal method is that the virtual method inherits the Override keyword and allows overloading, that is, modifying the method body.

The last sentence: please stick to your dream.

Three ways to abstract a class

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.