正在阅读:初探c#(十)界面(Interfaces)初探c#(十)界面(Interfaces)

2004-02-14 09:34 出处:PConline 作者:依栏望海 责任编辑:pjl
1.10 界面(Interfaces)   界面用来定义一种程序的契约。有了这个契约,就可以跑开编程语言的限制了(理论上)。而实现界面的 类或者结构要与界面的定义严格一致。界面可以包含以下成员:方法、属性、索引和事件。例子:*/ interface IExample { string this[int index] { get; set; } event EventHandler E; void F(int value); string P { get; set; } } public delegate void EventHandler(object sender, Event e); /*   例子中的界面包含一个索引、一个事件E、一个方法F和一个属性P。   界面可以支持多重继承。就像在下例中,界面“IComboBox”同时从“ITextBox”和“IListBox”继承。 */ interface IControl { void Paint(); } interface ITextBox: IControl { void SetText(string text); } interface IListBox: IControl { void SetItems(string[] items); } interface IComboBox: ITextBox, IListBox {} /*   类和结构可以多重实例化界面。 就像在下例中,类“EditBox”继承了类“Control”,同时从“IDataBound”和“IControl”继承。 */ interface IDataBound { void Bind(Binder b); } public class EditBox: Control, IControl, IDataBound { public void Paint(); public void Bind(Binder b) {...} } /*   在上面的代码中,“Paint”方法从“IControl”界面而来;“Bind”方法从“IDataBound”界面而来,都以“public”的身份在“EditBox”类中实现。

相关文章

关注我们

最新资讯离线随时看 聊天吐槽赢奖品