First, virtual method (virtual method)
The virtual keyword is used to decorate a method in a base class. There are two things you can do with virtual:
Scenario 1: The virtual method is defined in the base class, but the virtual method is not overridden in the derived class. In a call to a derived class instance, the virtual method uses the method defined by the base class.
Scenario 2: The virtual method is defined in the base class and then overridden in a derived class by using override. In a call to a derived class instance, the virtual method uses the derived overridden method.
When a method is declared as virtual, it is a dummy method until you use ClassName variable = new ClassName (); Before declaring an instance of a class, it does not exist in the real memory space. This keyword is very common in class inheritance and is used to provide polymorphism support for class methods.
Ii. Abstract Method (Abstraction method)
The abstract keyword can only be used to decorate a method in an abstraction class, and there is no specific implementation. An implementation of an abstract method must be implemented in a derived class by using the override keyword.
Abstract method declarations are used as methods that must be overridden by derived classes, which are used to be inherited, and can be seen as virtual methods that do not implement a body, and if the class contains abstract methods, the class must be defined as an abstract class, whether or not it contains other general methods, and the abstract class cannot have entities.
Three, polymorphic
The implementation of polymorphism in C # is divided into two types, one is compile-time polymorphism and one is run-time polymorphic
|-compile-time polymorphism through the overloads of multiple methods in a class, the system at compile time, according to the parameters passed to determine which overloaded method of the specific call;
|-Runtime polymorphism by virtual function (virtual function), abstract method implementation of polymorphism, derived classes to override virtual functions or abstract methods, so as to achieve runtime polymorphism.
Iv. usage of abstract
class files: public abstract class book { // Abstract method, without body, the class of abstract method must be abstract class, derived class must implement the method public abstract String in (); } public class javabook : book { string str = null; public javabook () { } public javabook (STRING STR) { this.str = str; } //implement abstract methods, must be implemented, note! The Override keyword must be added public override string in () &nbSp { return "zhaojianwei: " +STR; } } public class NETBOOK : book { String str = null; public NETBOOK () { } public netbook (STRING STR) { this.str = str; } //implementation of abstract methods, must be implemented, note! The Override keyword must be added public override string in ()  &Nbsp; { return "fullbuster: " +str; }  .} Main Page Face:public class text { public string zext () { //netbook nb = new netbook (); //return nb. In (); //Call book bk = new netbook ("Sheng");             BOOK BK1 = new netbook ("Zhao"); RETURN BK. In (); //Call } }public partial class default : system.web.ui.page { protected void page_load (object sender, eventargs e) { text t = new text (); Response.Write (T.zext ()); } }
v. Usage of virtual and the use of overrideclass file:public class anime { public virtual string ab () { return "Angel beats"; } public virtual string sao () { return "Sword art online"; } } public class A:Anime { public override string ab () { return "Playing で"; } //This method has not been rewritten public string sao () { return "link start!"; } }
Main side: Public class text { public string aext () { // a anime1 = new a (); // Return anime1. AB () + anime1. SAO (); anime anime = new a (); return anime. AB () +anime. SAO (); } }public partial class default : system.web.ui.page { protected void page_load (object sender, eventargs e) { TEXT&NBSP;T&NBSP;=&NBSP; New text (); response.write (T.aext () + "<br/>"); } }
Vi. the difference between virtual and abstract
(1), virtual modification of the method must be implemented (even if only to add a pair of curly braces), and the method of the abstract modification must not be implemented.
(2), virtual can quilt class rewrite, and the abstract must be overridden by the quilt class, if you override the virtual decoration method, the previous must add override (so that the compiler you want to rewrite the virtual method), and must be implemented, or compile error;
(3), if the class member is abstract decorated, then the class must be added abstract, because only abstract classes can have an abstract method.
(4), cannot create an instance of the abstract class, can only be inherited cannot be instantiated, such as: BaseTest2 base2 = new BaseTest2 (); A compilation error will occur: An abstract class or interface cannot create an instance.
(5), C # If you want to override a method in a subclass, you must add virtual to the parent class method before the subclass method, adding override before the child class, thus avoiding the programmer accidentally overriding the parent class method in the subclass.
(6), the abstract method must be overridden, and the virtual method must be implemented (even if it is a method defined in the abstract class).
Virtual method (virtual method) and abstract method (abstract method)