Effective C # Principle 20: Distinguishing between interface implementations and virtual function overloads

Source: Internet
Author: User

A cursory look, the feeling that implementation interface and virtual function overload is the same. You define objects, but these objects are stated in another type. You are deceived by the first feeling, implementation interface and virtual function overload is completely different. Members defined in an interface by default, there is no actual content at all.

A derived class cannot overload an interface member in a base class. Interfaces can be implemented implicitly, by hiding them from the public interface of the class. Their concepts are different and are used differently.

But you can implement interfaces like this: Allow your derived classes to modify your implementation. You just have to do a hook on a derived class. I believe that those who wrote the C + + program know what the hook means, and I really don't think it is good to translate the hook into anything, so just use the original word of hook, just like a bug. )

To demonstrate their differences, try to do a simple interface and implement it in a class:

interface IMsg
{
 void Message();
}
public class MyClass : IMsg
{
 public void Message()
 {
  Console.WriteLine( "MyClass" );
 }
}

The message () method is a MyClass public interface, and the message can also be accessed using an interface pointer imsg. Now let's get a little complicated, add a derived class:

public class MyDerivedClass : MyClass
{
 public new void Message()
 {
   Console.WriteLine( "MyDerivedClass" );
 }
}

Notice that I added a keyword new on the message method, which distinguishes one of the preceding message (see Principle 29). Myclass.message () is not a virtual function, and derived classes may not provide overloaded versions. The Myderived class creates a new message method, but this method is not overloaded myclass.message: It hides the original method. Moreover, Myclass.message can still use the imsg reference to ask:

MyDerivedClass d = new MyDerivedClass( );
d.Message( ); // prints "MyDerivedClass".
IMsg m = d as IMsg;
m.Message( ); // prints "MyClass"

The interface method is not virtual, and when you implement an interface, you need to declare the specific implementation content in a detailed correlation type.

But you might want to create interfaces, implement them in the base class, and modify their behavior in the derived class. This is a possible way to go. You have two choices, and if you do not access the base class, you can implement this interface again in a derived class:

public class MyDerivedClass : MyClass, IMsg
{
 public new void Message()
 {
   Console.WriteLine( "MyDerivedClass" );
 }
}

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.