Interface 动物 '总的来说:就是动物 会叫! ReadOnly Property 动物名() As String Sub 叫一声() End InterfacePublic Class Dog Implements 动物 Public Sub 叫一声() Implements 动物.叫一声 Console.WriteLine("汪,汪!") End Sub Public ReadOnly Property 动物名() As String Implements 动物.动物名 Get Return "狗" End Get End Property End ClassPublic Class Cat Implements 动物 Public Sub 叫一声() Implements 动物.叫一声 Console.WriteLine("喵,喵!") End Sub Public ReadOnly Property 动物名() As String Implements 动物.动物名 Get Return "猫" End Get End Property End Class
上面定义了2接口这里测试代码 Dim d As New Dog d.叫一声() Dim c As New Cat c.叫一声() Console.ReadLine()
Interface 动物
'总的来说:就是动物 会叫!
ReadOnly Property 动物名() As String
Sub 叫一声()
End InterfacePublic Class Dog
Implements 动物 Public Sub 叫一声() Implements 动物.叫一声
Console.WriteLine("汪,汪!") End Sub Public ReadOnly Property 动物名() As String Implements 动物.动物名
Get
Return "狗"
End Get
End Property
End ClassPublic Class Cat
Implements 动物 Public Sub 叫一声() Implements 动物.叫一声
Console.WriteLine("喵,喵!") End Sub Public ReadOnly Property 动物名() As String Implements 动物.动物名
Get
Return "猫"
End Get
End Property
End Class
Dim d As New Dog
d.叫一声()
Dim c As New Cat
c.叫一声()
Console.ReadLine()
《C#程序设计》不错的
ms-help://MS.VSCC/MS.MSDNVS.1028/csref/html/vcrefTheInterfaceType.htm
===========================
一个接口定义一个协定。实现接口的类或结构必须遵守其协定。接口可以包含方法、属性、索引器和事件作为成员。示例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);
显示了一个包含索引器、事件 E、方法 F 和属性 P 的接口。接口可以使用多重继承。在下面的示例中,interface IControl
{
void Paint();
}
interface ITextBox: IControl
{
void SetText(string text);
}
interface IListBox: IControl
{
void SetItems(string[] items);
}
interface IComboBox: ITextBox, IListBox {}
接口 IComboBox 同时从 ITextBox 和 IListBox 继承。类和结构可以实现多个接口。在下面的示例中,interface IDataBound
{
void Bind(Binder b);
}
public class EditBox: Control, IControl, IDataBound
{
public void Paint() {...}
public void Bind(Binder b) {...}
}
类 EditBox 从类 Control 派生,并且同时实现 IControl 和 IDataBound。在前面的示例中,IControl 接口中的 Paint 方法和 IDataBound 接口中的 Bind 方法是使用 EditBox 类的公共成员实现的。C# 提供了另一种方式来实现这些方法,使得实现类避免将这些成员设置成公共的。这就是:接口成员可以用限定名来实现。例如,在 EditBox 类中将 Paint 方法命名为 IControl.Paint,将 Bind 方法命名为 IDataBound.Bind 方法。public class EditBox: IControl, IDataBound
{
void IControl.Paint() {...}
void IDataBound.Bind(Binder b) {...}
}
用这种方式实现的接口成员称为显式接口成员,这是因为每个成员都显式地指定要实现的接口成员。显式接口成员只能通过接口来调用。例如,在 EditBox 中实现的 Paint 方法只能通过强制转换为 IControl 接口来调用。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
}
}
希望大家指点
我的MSN;[email protected]