8.9 Interfaces
来源:互联网 发布:马拉多纳 知乎 编辑:程序博客网 时间:2024/05/18 22:41
8.9 Interfaces
An interface defines a contract. A class or struct that implements an
interface must adhere to its contract.
Interfaces can contain methods, properties, events, and indexers as members.
The example
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, EventArgs e);
shows an interface that contains an indexer, an event E, a method F, and a
property P.
Interfaces may employ multiple inheritance. In the example
interface IControl
{
void Paint();
}
Chapter 8 Language Overview
43
interface ITextBox: IControl
{
void SetText(string text);
}
interface IListBox: IControl
{
void SetItems(string[] items);
}
interface IComboBox: ITextBox, IListBox {}
the interface IComboBox inherits from both ITextBox and IListBox.
Classes and structs can implement multiple interfaces. In the example
interface IDataBound
{
void Bind(Binder b);
}
public class EditBox: Control, IControl, IDataBound
{
public void Paint() {.}
public void Bind(Binder b) {.}
}
the class EditBox derives from the class Control and implements both
IControl and IDataBound.
In the previous example, the Paint method from the IControl interface and
the Bind method from
IDataBound interface are implemented using public members on the EditBox
class. C# provides an
alternative way of implementing these methods that allows the implementing
class to avoid having these
members be public. Interface members can be implemented using a qualified
name. For example, the
EditBox class could instead be implemented by providing IControl.Paint and
IDataBound.Bind
methods.
public class EditBox: IControl, IDataBound
{
void IControl.Paint() {.}
void IDataBound.Bind(Binder b) {.}
}
Interface members implemented in this way are called explicit interface
members because each member
explicitly designates the interface member being implemented. Explicit
interface members can only be
called via the interface. For example, the EditBox.s implementation of the
Paint method can be called
only by casting to the IControl interface.
class Test
{
static void Main() {
EditBox editbox = new EditBox();
editbox.Paint(); // error: no such method
IControl control = editbox;
control.Paint(); // calls EditBox.s Paint implementation
}
}
An interface defines a contract. A class or struct that implements an
interface must adhere to its contract.
Interfaces can contain methods, properties, events, and indexers as members.
The example
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, EventArgs e);
shows an interface that contains an indexer, an event E, a method F, and a
property P.
Interfaces may employ multiple inheritance. In the example
interface IControl
{
void Paint();
}
Chapter 8 Language Overview
43
interface ITextBox: IControl
{
void SetText(string text);
}
interface IListBox: IControl
{
void SetItems(string[] items);
}
interface IComboBox: ITextBox, IListBox {}
the interface IComboBox inherits from both ITextBox and IListBox.
Classes and structs can implement multiple interfaces. In the example
interface IDataBound
{
void Bind(Binder b);
}
public class EditBox: Control, IControl, IDataBound
{
public void Paint() {.}
public void Bind(Binder b) {.}
}
the class EditBox derives from the class Control and implements both
IControl and IDataBound.
In the previous example, the Paint method from the IControl interface and
the Bind method from
IDataBound interface are implemented using public members on the EditBox
class. C# provides an
alternative way of implementing these methods that allows the implementing
class to avoid having these
members be public. Interface members can be implemented using a qualified
name. For example, the
EditBox class could instead be implemented by providing IControl.Paint and
IDataBound.Bind
methods.
public class EditBox: IControl, IDataBound
{
void IControl.Paint() {.}
void IDataBound.Bind(Binder b) {.}
}
Interface members implemented in this way are called explicit interface
members because each member
explicitly designates the interface member being implemented. Explicit
interface members can only be
called via the interface. For example, the EditBox.s implementation of the
Paint method can be called
only by casting to the IControl interface.
class Test
{
static void Main() {
EditBox editbox = new EditBox();
editbox.Paint(); // error: no such method
IControl control = editbox;
control.Paint(); // calls EditBox.s Paint implementation
}
}
- 8.9 Interfaces
- Interfaces
- Interfaces
- interfaces
- 20. Interfaces
- IObjectSafety (Interfaces)
- Designing Interfaces
- IObjectSafety (Interfaces)
- 《Designing Interfaces》
- ubuntu interfaces
- Device Interfaces
- Lesson: Interfaces
- Android Interfaces
- GPIO Interfaces
- Python Interfaces are not Java Interfaces
- hacking SecureCRT Script Interfaces
- Interfaces vs Abstract Classes...
- Very Dynamic Web Interfaces
- 8.7.8 Instance constructors
- 8.7.9 Destructors
- 8.7.10 Static constructors
- 8.7.11 Inheritance
- 8.8 Structs
- 8.9 Interfaces
- 8.10 Delegates
- 简单防火墙(iptables)搭建(先写个框框,..)
- Delete不了(SQL)
- MSSQL版本[转]
- 理解和使用aix的日志系统
- 获取SQL连接参数的好方法
- [解决问题][不能更新eclipse]错误提示:Error retrieving "feature.xml". [error in opening zip file]
- iptables使用测试指令