什么时候该用interface?

pjlu1982 2003-06-13 10:53:34
我刚学c#不久,看到了interface,
但是不知道什么时候该用interface?他具体起什么作用?
最好能给个小例子!!
还有,property有什么用?
谢谢~~
...全文
93 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
jiangym 2003-06-13
  • 打赏
  • 举报
回复
interface,一是不同的程序需要互相通信,进行缝合时做的契约,二是与COM进行交互时用到.
前者如,编写代码,最终允许在银行账户之间进行计算机转帐业务.
很多公司都可以实现银行帐户,但它们之间需要互相通信以便账户之间进行转帐,这需要一个相互约定,这时这个约定就用接口来实现了。设接口为IbankAccount,它包含一个用于存取款的方法,一个返回余额的属性,还允许外部代码识别由不同银行帐户执行的各种银行帐户类。

下面我给个例子,不过也是从书上看到的,你可以直接找《C#高级编程》这本书来看。
namespace Mytest.BankProtocols
{
public interface IBankAccount
{
void PayIn(decimal amount);
bool Withdraw(decimal amount);

decimal Balance
{
get;
}
}
}

//一个类
namespace Mytest.VenusBank
{
public class SaveAccount:IBankAccount
{
private decimal balance;

public void PayIn(decimal amount)
{
balance += amount;
}

public bool Withdraw(decimal amount)
{
if (balance >= amount)
{
balance -= amount;
return true;
}
Console.WriteLine("Withdrawal attempt failed.");
return false;
}

public decimal Balance
{
get
{
retrun balance;
}
}

public override string ToString()
{
return String.Format("Venus Bank Saver:Balance = {0,6:C}",balance);
}
}
}

//另一个类
namespace Mytest.JupiterBank
{
public class GodAccount:IBankAccount
{
private decimal leavings;

public void PayIn(decimal amount)
{
leavings += amount + 0.01;
}

public bool Withdraw(decimal amount)
{
if (leavings >= amount)
{
leavings -= amount;
return true;
}
Console.WriteLine("Withdrawal attempt failed.");
return false;
}

public decimal Balance
{
get
{
retrun leavings;
}
}

public override string ToString()
{
return String.Format("JupiterBank Saver:Balance = {0,6:C}",leavings);
}

}
}

chsl918 2003-06-13
  • 打赏
  • 举报
回复
interface:
接口表示一种约定,实现接口的类必须严格按其定义来实现接口的每个方面。

有了接口,就可以将功能定义为一些紧密相关成员的小组。可以在不危害现有代码的情况下,开发接口的增强型实现,从而使兼容性问题最小化。也可以在任何时候通过开发附加接口和实现来添加新的功能。

虽然接口实现可以进化,但接口本身一旦被发布就不能再更改。对已发布的接口进行更改会破坏现有的代码。若把接口视为约定,很明显约定双方都各有其承担的义务。接口的发布者同意不再更改该接口,接口的实现者则同意严格按设计来实现接口。


接口实现示例
实现接口的类必须实现其所有的属性、方法和事件。

以下示例定义了两个接口。第二个接口 Interface2 对 Interface1 做了继承,且定义了一个附加属性和方法。

Interface Interface1
Sub sub1(ByVal i As Integer)
End Interface

Interface Interface2
Inherits Interface1 ' Demonstrates interface inheritance.
Sub M1(ByVal y As Integer)
ReadOnly Property Num() As Integer
End Interface
下一个示例则实现上一示例中所定义的接口 Interface1:

Public Class ImplementationClass1
Implements Interface1
Sub Sub1(ByVal i As Integer) Implements Interface1.Sub1
' Place code here to implement this method.
End Sub
End Class
最后一个示例则实现 Interface2,包括自 Interface1 继承的方法:

Public Class ImplementationClass2
Implements Interface2
Dim INum As Integer = 0
Sub sub1(ByVal i As Integer) Implements Interface2.Sub1
' Place code here that implements this method.
End Sub
Sub M1(ByVal x As Integer) Implements Interface2.M1
' Place code here to implement this method.
End Sub

ReadOnly Property Num() As Integer Implements _
Interface2.Num
Get
Num = INum
End Get
End Property
End Class



Property 对象
表示给定对象的属性一般集合中的一个属性。
示例
Sub PropertyExample()
Dim Props As Properties
Dim PropObj As [Property]
Dim NameValPair As String

Props = DTE.Properties("Environment", "General")
MsgBox("Tools – Options – Environment – General Properties Count = " & Props.Count())
For Each PropObj In Props
NameValPair = NameValPair & (PropObj.Name & "Value = " & PropObj.Value & microsoft.VisualBasic.ControlChars.CrLf)
Next
MsgBox(NameValPair)
End Sub

以上全部从MSDN中找到的,所以MSDN还是不错的。有不少例子和解释

110,570

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧