接口与抽象类(通用OO) [英] Interface vs Abstract Class (general OO)

查看:136
本文介绍了接口与抽象类(通用OO)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近有两次电话采访,我被问及接口和抽象类之间的区别。我已经解释了他能想到的每一个方面,但似乎他们在等我提一些具体的东西,我不知道它是什么。

I have had recently two telephone interviews where I've been asked about the differences between an Interface and an Abstract class. I have explained every aspect of them I could think of, but it seems they are waiting for me to mention something specific, and I don't know what it is.

根据我的经验,我认为以下是正确的。如果我错过了一个重点,请告诉我。

From my experience I think the following is true. If I am missing a major point please let me know.

接口:

接口中声明的每个方法都必须在子类中实现。
只有事件,代理,属性(C#)和方法可以存在于接口中。一个类可以实现多个接口。

Every single Method declared in an Interface will have to be implemented in the subclass. Only Events, Delegates, Properties (C#) and Methods can exist in a Interface. A class can implement multiple Interfaces.

抽象类:

仅抽象方法必须由子类实现。 Abstract类可以有实现的常规方法。 Abstract类还可以在Events,Delegates,Properties和Methods旁边有类变量。一个类只能实现一个抽象类,因为C#中不存在多重继承。

Only Abstract methods have to be implemented by the subclass. An Abstract class can have normal methods with implementations. Abstract class can also have class variables beside Events, Delegates, Properties and Methods. A class can only implement one abstract class only due non-existence of Multi-inheritance in C#.


  1. 毕竟,面试官提出了一个问题如果你有一个只有抽象方法的抽象类怎么办?那么它与界面会有什么不同?我不知道答案,但我认为这是上面提到的继承权吗?

  1. After all that, the interviewer came up with the question "What if you had an Abstract class with only abstract methods? How would that be different from an interface?" I didn't know the answer but I think it's the inheritance as mentioned above right?

另一位面试官问我,如果你有一个公共变量怎么办?接口,这与Abstract Class有什么不同?我坚持认为你不能在界面中有一个公共变量。我不知道他想听到什么,但他也不满意。

An another interviewer asked me what if you had a Public variable inside the interface, how would that be different than in Abstract Class? I insisted you can't have a public variable inside an interface. I didn't know what he wanted to hear but he wasn't satisfied either.

参见

接口与抽象类

你如何决定使用抽象类和接口?

接口和抽象类之间有什么区别?

推荐答案

虽然你的问题表明它是针对普通OO的,但它似乎真正关注.NET对这些术语的使用。

While your question indicates it's for "general OO", it really seems to be focusing on .NET use of these terms.

在.NET中(类似) for Java):

In .NET (similar for Java):


  • 接口没有状态或实现

  • 实现接口的类必须提供该接口的所有方法的实现

  • 抽象类可能包含状态(数据成员)和/或实现(方法)

  • abstract可以在不实现抽象方法的情况下继承类(虽然这样的派生类本身就是抽象的)

  • 接口可能是多重继承的,抽象类可能不是(这可能是关键的具体原因)接口与abtract类分开存在 - 它们允许实现多重继承,消除了一般MI的许多问题。)

  • interfaces can have no state or implementation
  • a class that implements an interface must provide an implementation of all the methods of that interface
  • abstract classes may contain state (data members) and/or implementation (methods)
  • abstract classes can be inherited without implementing the abstract methods (though such a derived class is abstract itself)
  • interfaces may be multiple-inherited, abstract classes may not (this is probably the key concrete reason for interfaces to exist separately from abtract classes - they permit an implementation of multiple inheritance that removes many of the problems of general MI).

As一般的OO术语,差异不一定是明确的。例如,有些C ++程序员可能持有类似的严格定义(接口是抽象类的严格子集,不能包含实现),而有些人可能会说具有一些默认实现的抽象类仍然是接口或非抽象类class仍然可以定义一个接口。

As general OO terms, the differences are not necessarily well-defined. For example, there are C++ programmers who may hold similar rigid definitions (interfaces are a strict subset of abstract classes that cannot contain implementation), while some may say that an abstract class with some default implementations is still an interface or that a non-abstract class can still define an interface.

确实,有一种称为非虚拟接口(NVI)的C ++习语,其中公共方法是非虚拟方法'到私人虚拟方法:

Indeed, there is a C++ idiom called the Non-Virtual Interface (NVI) where the public methods are non-virtual methods that 'thunk' to private virtual methods:

  • http://www.gotw.ca/publications/mill18.htm
  • http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Non-Virtual_Interface

这篇关于接口与抽象类(通用OO)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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