您的位置:首页 > 其它

显式接口成员实现

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

    public class Binder


[code]{


//


}


public interface IControl


{


void Paint();


}


public interface IDataBind


{


void Bind(Binder b);


}


public class EditBox : IControl,IDataBind


{


#region IControl Members


 


public void IControl.Paint()


{


throw new NotImplementedException();


}


 


#endregion


 


#region IDataBind Members


 


public void IDataBind.Bind(Binder b)


{


throw new NotImplementedException();


}


 


#endregion


}


调用方法:

[/code]

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

        static void Main(string[] args)


[code]{


IControl editBox = new EditBox();


editBox.Paint();//正确


EditBox edit = new EditBox();


edit.Paint();//错误,找不到方法


}

[/code]

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: