Interface implementation Inheritance Mechanism

Source: Internet
Author: User

Interface implementation Inheritance Mechanism

A class inherits the implementation of interfaces provided by its base class:

Note: If you do not explicitly implement the interface again, the derived class cannot change the interface ing inherited from the base class.

Using System;


Interface IControl

{

Void Paint ();

}


Class Control: IControl

{

Public void Paint ()

{

Console. WriteLine ("Control invoke! ");

}

}


Class TextBox: Control

{

New public void Paint ()

{

Console. WriteLine ("TextBox invoke! ");

}

}


Class Invoke

{

Public static void Main ()

{

Control c = new Control ();

TextBox tb = new TextBox ();

IControl ic = c;

IControl it = tb;

C. Paint (); // invokes Control. Paint ();

Tb. Paint (); // invokes TextBox. Paint ();

Ic. Paint (); // invokes Control. Paint ();

It. Paint (); // invokes Control. Paint ();

}

}


 

However, when an interface method is mapped to a virtual method in the class, the derived class can overload this virtual method and change the implementation of this interface:

Interface IControl

{

Void Paint ();

}


Class Control: IControl

{

Public virtual void Paint ()

{


}

}


Class TextBox: Control

{

Public override void Paint ()

{


}

}


Code effect:

Control c = new Control ();

TextBox tb = new TextBox ();

IControl ic = c;

IControl it = tb;

C. Paint (); // call Control. Paint ();

Tb. Paint (); // call TextBox. Paint ();

Ic. Paint (); // call Control. Paint ();

It. Paint (); // call TextBox. Paint ();


Because the explicitly stated interface members cannot be declared as virtual, the implementation of explicitly stated interfaces cannot be reloaded:

Note: We recommend that you use an explicitly stated interface to call another method. The called method can be declared as virtual and allowed to be reloaded by the derived class.

Interface IControl

{

Void Paint ();

}


Class Control: IControl

{

Void IControl. Paint ()

{

PaintControl ();

}

Protected virtual void PaintControl ()

{


}

}


Class TextBox: Control

{

Protected override void PaintControl ()

{


}

}

Here, classes derived from Control can be implemented by reloading the PaintControl method. Paint.

Referenced from:

Http://www2.cnblogs.com/netfork/archive/2004/03/23/3930.html

 

 


 

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.