[Go] C # hiding methods and overriding methods

Source: Internet
Author: User

1: Method overrides: The method in the base class is identified with the virtual keyword and then overridden in the inheriting class

(override), so that the methods in the base class have been overridden in the subclass, and the methods in the base class have lost functionality in the subclass

The When a reference to an object of a base class is directed directly to an object of the inheriting class (polymorphism), calling the method is a heavy

Write the method.

2: Method hiding: The New keyword can be used in an inheriting class, regardless of whether the method in the base class uses the virtual keyword (if the new

does not produce an error, but generates a compilation warning) hides the methods in the base class so that hidden is hidden, not as heavy

Write, overriding is that the old method in the base class already does not exist in the subclass, while hiding is the method in the base class and the overridden in the subclass of the new

Methods are present. So when a reference to an object of a base class is directed directly to an object of the inheriting class (polymorphism), calling the method invokes the

The method of the base class.

classBaseClass { Public voidFunctiona () {Console.WriteLine ("Basefunctiona = = ="); }           Public Virtual voidfunctionb () {Console.WriteLine ("Basefunctionb = = ="); }     }     classDerivedclass:baseclass {New  Public voidFunctiona () {Console.WriteLine ("Derivedfunctiona = = ="); }          Public Override voidfunctionb () {Console.WriteLine ("Derivedfunctionb = = ="); }     }    classProgram {Static voidMain (string[] args) {BaseClass B2=NewDerivedClass (); B2.functiona ();//Basefunctiona = = =B2.functionb ();//Derivedfunctionb = = =Console.WriteLine ("-----"); BaseClass b=NewBaseClass (); B.functiona ();//Basefunctiona = = =B.functionb ();//Basefunctionb = = =Console.WriteLine ("-----"); DerivedClass d=NewDerivedClass (); D.functiona ();//Derivedfunctiona = = =D.functionb ();//Derivedfunctionb = = =Console.WriteLine ("-----"); }          }

Source: http://blog.51cto.com/745559847/1857264

[Go] C # hiding methods and overriding methods

Related Article

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.