接口 vs 抽象类(一般面向对象) [英] Interface vs Abstract Class (general OO)

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

问题描述

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

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.

抽象类:

子类只需要实现抽象方法.抽象类可以具有带有实现的普通方法.除了事件、委托、属性和方法之外,抽象类还可以有类变量.由于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?

另一位面试官问我,如果接口中有一个 Public 变量会怎样,这与抽象类中有什么不同?我坚持认为接口中不能有公共变量.我不知道他想听什么,但他也不满意.

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 中(类似于 Java):

In .NET (similar for Java):

  • 接口不能有状态或实现
  • 实现接口的类必须提供该接口所有方法的实现
  • 抽象类可能包含状态(数据成员)和/或实现(方法)
  • 抽象类可以在不实现抽象方法的情况下被继承(尽管这样的派生类本身是抽象的)
  • 接口可能是多重继承的,抽象类可能不是(这可能是接口与抽象类分开存在的关键具体原因——它们允许实现多重继承,从而消除了一般 MI 的许多问题).

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

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.

确实,有一个 C++ 习惯用法称为非虚拟接口 (NVI),其中公共方法是转换"为私有虚拟方法的非虚拟方法:

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:

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

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