A. Overriding a method of a parent class to use the Override keyword (a method with the Override keyword adornment is a new implementation of a method of the same name in the parent class)
B. To override the method of the parent class, provided that the method in the parent class that is being overridden must be declared as virtual or an abstract type. to the parent class
The addition of the virtual keyword to the method to be overridden means that its implementation can be overridden in a subclass. (Note: Methods in C # are not the default
Virtual type, so the virtual keyword is added to be overridden)
The C.virtual keyword is used to define a method as supporting polymorphism, and a method with the virtual keyword decoration is called a "dummy method"
[Access modifier] virtual [return type] Method name (parameter list)
{
Implementation of the virtual method, the method can be overridden by the quilt class
}
namespaceconsoleapplication1{classEmployee { PublicEmployee () {Console.WriteLine ("how to construct a parent class"); } Public Virtual voidEmpinfo () {Console.WriteLine ("the virtual method of the parent class"); } } classDervemployee:employee { PublicDervemployee () {Console.WriteLine ("how to construct subclasses"); } Public Override voidEmpinfo () {Base. Empinfo ();//The base keyword will be mentioned in the following extensionConsole.WriteLine ("overriding methods for subclasses"); } } classProgram {Static voidMain (string[] args) {Dervemployee Objdervemployee=NewDervemployee (); Objdervemployee.empinfo (); //Note: an instance of the Objdervemployee derived class is a reference to the objemployee that is assigned to the employee class .//so objemployee refers to methods that call the Empinfo () method or call the Dervemployee classEmployee Objemployee =NewEmployee (); Objemployee.empinfo (); } }}
C # Overrides