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