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.



The code is as follows:


Class BaseClass

{

public void Functiona ()

{

Console.WriteLine ("Basefunctiona = = =");

}

public virtual void functionb ()

{

Console.WriteLine ("basefunctionb = = =");

}

}

Class Derivedclass:baseclass

{

New public void Functiona ()

{

Console.WriteLine ("Derivedfunctiona = = =");

}

public override void Functionb ()

{

Console.WriteLine ("derivedfunctionb = = =");

}

}


Class Program

{

static void Main (string[] args)

{

BaseClass b2 = new DerivedClass ();

B2.functiona ();//basefunctiona = = =

B2.functionb ();//derivedfunctionb = = =

Console.WriteLine ("-----");


BaseClass B = new BaseClass ();

B.functiona ();//basefunctiona = = =

B.functionb ();//basefunctionb = = =

Console.WriteLine ("-----");


DerivedClass d = new DerivedClass ();

D.functiona ();//derivedfunctiona = = =

D.functionb ();//derivedfunctionb = = =

Console.WriteLine ("-----");

}

}


C # hiding methods and overriding methods

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.