这样的感觉很正常。使用接口的最大优点是它们将多态性的使用扩展到类层次结构之外。它们 通常也用于创建小框架。以下是两个例子: - 假设您有责任为银行编写ATM申请。 通过您的申请,您将允许用户存款,提款并检查 他们的余额用于支票账户和储蓄账户。无论您的应用程序处理什么类型的帐户,它都需要执行那些树 操作。您可以确保从包含必要成员的抽象 BankAccount中获取支票和储蓄。现在想象一下,在 的未来,您的ATM应用程序也可能用于访问信用卡账户 并且CreditCard类与 BankAccounts。您仍然需要强制它存款,提取 和余额成员。界面是这里的完美解决方案。 - 另一个真实的例子。您可能知道 Array类的Sort方法。它在单维数组中对数据进行排序。请记住,数组 可以是任何类型:整数,字符串,动物,BankAccounts等.Autline 方法不关心数据类型。它通过IComparable接口访问底层对象 。因此,为了使您的类可排序,您需要实现IComparable接口。这与 继承无关。 我希望这有点帮助。 Telmo Sampaio MCT " Frank Rizzo" <无** @ none.com>写在消息 新闻:ey ************** @ tk2msftngp13.phx.gbl ...Hi Frank,It is normal to feel like that. The big advantage of using interfaces isthat they extend the use of polymorphism beyond the class hierarchy. Theyare normally used to create small frameworks as well. Here are two examples:- imagine you are responsible for writing an ATM application for a bank.Thru your application you will allow users to Deposit, Withdraw and checktheir Balance for Checking accounts and Savings Accounts. No matter whatkind of account your application handles, it needs to execute those treeoperations. You can sure derive Checking and Savings from an abstractBankAccount that contain the necessary members. Now imagine that in thefuture, your ATM application may also be used to access Credit Card accountsand that the CreditCard class is not in the same inheritance hierarchy asthe BankAccounts. You still need to force it to have the Deposit, Withdrawand Balance members. An interface is the perfect solution here.- another real example. You are probably aware of the Sort method for theArray class. It sorts data in a single dimension array. Remember that arrayscan be of any type: integers, strings, Animals, BankAccounts, etc. The Sortmethod does not care about the data type. It access the underlying objectthru the IComparable interface. Therefore, to make your classes sortable,you need to implement the IComparable interface. This has nothing to do withinheritance.I hope this helps a little bit.Telmo SampaioMCT"Frank Rizzo" <no**@none.com> wrote in messagenews:ey**************@tk2msftngp13.phx.gbl...我们有一个工作中的宗教辩论:接口vs 课程。我的看法是,Classes为您提供了更大的灵活性。您可以通过标记方法抽象(这是接口所做的一切)来对后代类强制执行合同。此外,通过添加子类继承的功能,这些类允许您灵活。或者通过虚拟方法提供可选功能。 现在,我知道接口有它的位置,但我很难找到类的情况(通过继承)不提供相同或更好的解决方案。 我错过了什么?我愿意接受双方的争论。 问候。 We are having one of those religious debates at work: Interfaces vs Classes. My take is that Classes give you more flexibility. You can enforce a contract on the descendant classes by marking methods abstract (which is all that an interface does). In addition, the classes allow you to be flexible by adding functionality that child classes inherit. Or by providing optional functionality via virtual methods. Now, I understand that Interfaces have its place, but I am having a hard time finding a situation where the classes (via inheritance) do not provide an identical or better solution. Am I missing something? I am open to arguments from both sides. Regards.假设您想为您的应用程序制作插件架构 第三方开发人员可以为您的应用程序开发组件,以便在运行时使用。你愿意给他们上课吗?或者你会告诉他们关于界面的吗? :-) QED。Lets say you wanted to make a Plugin architecture to your applicationwhere 3rd party developers could develop components for your app to useat runtime. Would you be willing to give them the class or would yourather just tell them about the interface? :-)QED. Remoting。 你不想分发基于服务器的课程,只是这个班级的界面。 - 科林。 Frank Rizzo <无** @ none.com>写在消息 新闻:ey ************** @ tk2msftngp13.phx.gbl ...Remoting.You do not want to distribute server based classes, just the interface tothat class.- Colin."Frank Rizzo" <no**@none.com> wrote in messagenews:ey**************@tk2msftngp13.phx.gbl...我们有一个工作中的宗教辩论:接口vs 课程。我的看法是,Classes为您提供了更大的灵活性。您可以通过标记方法抽象(这是接口所做的一切)来对后代类强制执行合同。此外,通过添加子类继承的功能,这些类允许您灵活。或通过虚拟方法提供可选功能。 现在,我了解接口有它的位置,但我很难找到一种情况,即课程(通过继承)不提供相同或更好的解决方案。 我错过了什么?我愿意接受双方的争论。 问候。 We are having one of those religious debates at work: Interfaces vs Classes. My take is that Classes give you more flexibility. You can enforce a contract on the descendant classes by marking methods abstract (which is all that an interface does). In addition, the classes allow you to be flexible by adding functionality that child classes inherit. Or by providing optional functionality via virtual methods. Now, I understand that Interfaces have its place, but I am having a hard time finding a situation where the classes (via inheritance) do not provide an identical or better solution. Am I missing something? I am open to arguments from both sides. Regards. 这篇关于接口与类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-03 15:44