C # Overrides

Source: Internet
Author: User

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

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.