Coverage and hiding in C #

Source: Internet
Author: User

 

In C ++, overloading, overwriting, and hiding are confusing. When I first came into contact with C #, this usage also turned me around for a while, I have time to record my understanding of them today. please correct me.

In C ++, they are defined as follows:

Concept

Range

Parameters

Is the base class virtual?

Heavy Load In the same class

Different

Y/n

Override Inheritance

Same

Y

Hide hide Inheritance

Same

N

Different

Y/n

Override indicates that:
It mainly refers to the definition of a virtual function in the base class and the re-implementation of this function in the derived class. This is called overwriting or rewriting.
There are two scenarios to hide:
(1) If the function of the derived class has the same name as the function of the base class, but the parameter is different. In this case, functions of the base class will be hidden regardless of whether there is any virtual keyword (Be sure not to confuse them with overload ).
(2) If the function of the derived class has the same name and parameter as the function of the base class, but the base class function does not have the virtual keyword. At this time, the base class functions are hidden (Be sure not to confuse with overwrite)

Let's take a look at the example below:

1. Coverage

/**//*
* Coverage Method Testing
*/
Public class base_override ...{
Public Virtual void introduce ()...{
System. Console. writeline ("I'm base_override_introduce ()");
}

Public Virtual void sayhi ()...{
System. Console. writeline ("I'm base_override_sayhi ()");
}
}

Public class derive_override: base_override ...{
Public override void introduce ()
...{
System. Console. writeline ("I'm derive_override_introduce ()");
// Base. Introduce ();
}

// Note that this method does not have the override parent class Method
// Warning when compiling. We recommend that you add override or new
// This method is hidden by default in C #. In C ++, data overwrites should not be hidden.
Public void sayhi ()...{
System. Console. writeline ("I'm derive_override_sayhi ()");
}

Public void sayhi (bool B )...{
System. Console. writeline ("I'm derive_override_sayhi (bool B )");
}
}

 

The test code is as follows:

 

Base_override = new base_override ();
Derive_override = new derive_override ();
// Call introduce () in base_override ()
Base_override.introduce ();
// Call sayhi () in base_override ()
Base_override.sayhi ();
// Call introduce () in derive_override ()
Derive_override.introduce ();
// Call sayhi () in derive_override ()
Derive_override.sayhi ();
// Call sayhi (bool B) in derive_override)
Derive_override.sayhi (true );

Base_override = new derive_override ();
// Call introduce () in derive_override ()
Base_override.introduce ();
// Call sayhi () in base_override ()
// ** In C ++, sayhi () OK may still be in derive_override
Base_override.sayhi ();
// Call sayhi () in derive_override ()
(Derive_override) base_override). sayhi ();
// Compile error!
// Base_override.sayhi (true );

 

 

2. Hide

 

/**//*
* Test the hidden Method
*/
Public class base_new ...{
Public void introduce ()
...{
System. Console. writeline ("I'm base_new_introduce ()");
}

Public void sayhi ()
...{
System. Console. writeline ("I'm base_new_sayhi ()");
}
}

Public class derive_new: base_new ...{
// New is not added, but C # adds new to the default behavior
// Warning when compiling. We recommend that you add override or new
Public void introduce ()
...{
System. Console. writeline ("I'm derive_new_introduce ()");
}

// Add new explicitly. The actual effect is the same as that without new, but after adding new, compile warning is eliminated.
Public new void sayhi ()
...{
System. Console. writeline ("I'm derive_new_sayhi ()");
}

}

 

The test code is as follows:

 

Base_new = new base_new ();
Derive_new = new derive_new ();
// Call introduce () in base_new ()
Base_new.introduce ();
// Sayhi () in base_new will be called ()
Base_new.sayhi ();
// Call introduce () in derive_new ()
Derive_new.introduce ();
// Sayhi () in derive_new will be called ()
Derive_new.sayhi ();

Base_new = new derive_new ();
// Call introduce () in base_new ()
Base_new.introduce ();
// Sayhi () in base_new will be called ()
Base_new.sayhi ();

 

The differences between coverage and hiding in C # are described based on the test results. After the same test in C ++, the usage of C # And C ++ is still slightly different because C ++ does not have override and New Keyword constraints ..

The above may have some misunderstandings. Please correct them ..

 

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.