显式接口成员实现

类或结构可以通过使用显示接口实现来避免将成员声明为 public. 显示接口成员实现使用完全限定的接口成员名。例如 EidtBox类可以使用显示接口成员实现来实现IControl的Paint方法和IDataBind的Bind方法。

   1:      public class Binder
   2:      {
   3:          //
   4:      }
   5:      public interface IControl
   6:      {
   7:          void Paint();
   8:      }
   9:      public interface IDataBind
  10:      {
  11:          void Bind(Binder b);
  12:      }
  13:      public class EditBox : IControl,IDataBind
  14:      {
  15:          #region IControl Members
  16:   
  17:          public void IControl.Paint()
  18:          {
  19:              throw new NotImplementedException();
  20:          }
  21:   
  22:          #endregion
  23:   
  24:          #region IDataBind Members
  25:   
  26:          public void IDataBind.Bind(Binder b)
  27:          {
  28:              throw new NotImplementedException();
  29:          }
  30:   
  31:          #endregion
  32:      }
调用方法:
   1:          static void Main(string[] args)
   2:          {
   3:              IControl editBox = new EditBox();
   4:              editBox.Paint();//正确
   5:              EditBox edit = new EditBox();
   6:              edit.Paint();//错误,找不到方法
   7:          }
posted @ 2012-02-28 14:44  梅子黄时雨  阅读(239)  评论(0编辑  收藏  举报