C # Hide and override in. Net (keyword new and override)

Source: Internet
Author: User
/*
* Created by sharpdevelop.
* User: noo
* Date: 2009-8-16
* Time: 16: 40
*
* Hide and override (the keywords are new and override respectively)
*/

Using system;
Class newa
{
Public Virtual void output1 ()
{
Console. writeline ("base class method 1 ");
}
Public void output2 ()
{
Console. writeline ("base class method 2 ");
}
}
Class newb: newa
{
Public override void output1 () // the corresponding base class method must use the virtual keyword. This method is called to override the base class method.
{
Console. writeline ("override base class method 1 ");
}
New public void output2 () // the corresponding base class method can be used without the virtual keyword. This method is called to hide the base class method.
{
Console. writeline ("hiding base class method 2 ");
}
}
Class Test
{
Static void main ()
{
Newb B = new newb ();
Newa;
A = B;
A. output1 (); // call the rewrite method in B
A. output2 (); // call the original method in
B. output1 (); // call the rewrite method in B.
B. output2 (); // call the hidden method in B.
Newa AA = new newa ();
AA. output1 (); // call the original method in
AA. output2 (); // call the original method in
}
}

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.