接口和抽象类之间的差异 [英] Differences between interface and abstract class

查看:77
本文介绍了接口和抽象类之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在MSDN上经历了Interface和Abstract类的不同使用场景



http:// msdn .microsoft.com / zh-CN / library / scsyfw1d(v = vs.71).aspx



但是对于下面的差异有些困惑:



如果您预计要创建组件的多个版本,请创建一个抽象类。抽象类提供了一种简单易用的组件版本。通过更新基类,所有继承类都会随更改自动更新。另一方面,接口一旦创建就无法更改。如果需要新版本的界面,你必须创建一个全新的界面。



我对下面这个特别大胆的句子感到困惑:
$ b另一方面,$ b 一旦创建就无法更改。如果需要新版本的界面,则必须创建一个全新的界面



你能帮忙解释一下吗?

Hi All,
I was going through the different using scenarios for Interface and Abstract class on MSDN

http://msdn.microsoft.com/en-us/library/scsyfw1d(v=vs.71).aspx

but got some confusion on below difference:

If you anticipate creating multiple versions of your component, create an abstract class. Abstract classes provide a simple and easy way to version your components. By updating the base class, all inheriting classes are automatically updated with the change. Interfaces, on the other hand, cannot be changed once created. If a new version of an interface is required, you must create a whole new interface.

I have confusion on this Particular Bold Sentence below:
on the other hand, cannot be changed once created. If a new version of an interface is required, you must create a whole new interface

Could you help explain?

推荐答案

首先,关于差异的大多数问题都没有意义。你如何定义差异?如果你没有得到它,请告诉我们:apple和Apple之间的区别是什么?

您应该了解接口的工作方式以及基类的工作原理。从本质上讲,说实话,你需要了解OOP是如何工作的,因为问题是OOP的核心问题,所以完全回答它是毫无意义的。只需单独学习这两个东西,确保你理解两者的使用方法,然后再提出问题,如果有的话。



至于你引用的MSDN文章,你应该明白,尽管MSDN文档现在变得非常好,但是有些文章有误导性或不完整,有些领域根本没有涉及。不幸的是,它恰好涵盖了应该涵盖第一原则的主题,这与计算机科学的基本概念有关。所以,我的建议很简单:帮自己一个忙,完全无视这篇简短且没有信息的文章,特别是你用粗体引用的句子。老实说,这几乎是胡言乱语。让我们忘了它。



为了有用,你的问题应该是不同的:不是有什么区别?但是类似于:如何决定在何处使用抽象类和派生类的层次结构以及接口的位置以及将这些方法结合在一起的位置?这将使问题变得合理,但不是微不足道的,而且不幸的是,并非如此简单。请查看我过去的答案:

对接口的怀疑 [ ^ ],实际上,这个答案引用所有其他答案:

如何决定选择抽象类或接口 [ ^ ],

抽象类和接口之间的区别,如果它们具有相同的no方法和var [ ^ ],

当我们使用抽象时,当我们使用接口时......? [ ^ ],

接口和多态性 [ ^ ]。



- SA
First of all, most questions on the "difference" make no sense. How would you define the "difference"? If you did not get it, please tell us: what's the difference between apple and Apple?
You should just learn how interfaces work and how base classes work. In essence, to be honest, you need learn how OOP works, because the question is at the very heart of OOP, so just answering it exactly would be pretty pointless. Just learn both things separately, make sure you understand the use both approached, and then ask further questions, if you have to.

As to the MSDN article you referenced, you should understand that, even though MSDN documentation became very good these days, some articles are quite misleading or incomplete, and some areas are not covered at all. Unfortunately, it happens to the topics which are supposed to cover first principle, something related to fundamental notions of computer sciences. So, my advice is simple: do yourself a favor and completely disregard this short and not informative article, in particular, the sentence you quoted in bold. It's almost gibberish, honestly. Let's just forget about it.

To be useful, your question should be different: not "what's the difference"? but something like: "how to decide where to use abstract classes and hierarchy of derived classes and where the interfaces, and where to combine these approaches?" That would make the question sensible, but not trivial, and, unfortunately, not so simple. Please see my past answers:
Doubts on Interfaces[^], actually, this answer references all other answers:
How to decide to choose Abstract class or an Interface[^],
Difference between abstract class and interface if they have same no of methods and var[^],
When we use abstract and when we use interface...?[^],
Interfaces and Polymorphism[^].

—SA


抽象类是抽象成员和非抽象成员的组合,接口只包含抽象成员。
Abstract class is a combination of abstract and non abstract member and interface contain only abstract member.


带接口,如果你正计划改变一些事情。整个合同都会改变,你也必须改变你的实施。这就是你可以使用接口拥有多个版本组件的原因。只有一种方法可以继承新创建的现有接口。



使用Abstract类,您可以更改多个版本的行为或实现,并且可以有多个版本组件不会破坏,除非方法签名发生变化,你必须在所有版本中进行更改。
With Interfaces , if you are planning to change something. The whole contract gets changed and you will have to change your implementations too. That's the reason you can have multiple version of components using interfaces. There's only one way inherit the newly created interface with the existing one.

With Abstract class, You could change the behavior or the implementation for multiple versions and can have multiple versions of components so the code won't break unless the method signature changes you have to make a change in all versions.


这篇关于接口和抽象类之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆