“接口”的定义是什么?面向对象编程 [英] What is the definition of "interface" in object oriented programming

查看:168
本文介绍了“接口”的定义是什么?面向对象编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我的一个朋友在编程中对界面的含义进行反复讨论。

Ok, a friend of mine go back and forth on what "interface" means in programming.

界面的最佳描述是什么。

What is the best description of an "interface".

对我来说,一个接口是一个类的蓝图,这是最好的定义吗?

To me an interface is a blueprint of a class, is this the best definition?

推荐答案

接口是开发中比较重载且容易混淆的术语之一。

An interface is one of the more overloaded and confusing terms in development.

它实际上是一个抽象和封装的概念。对于给定的框,它声明该框的输入和输出。在软件世界中,这通常意味着可以在框中调用的操作(以及参数)以及在某些情况下这些操作的返回类型。

It is actually a concept of abstraction and encapsulation. For a given "box", it declares the "inputs" and "outputs" of that box. In the world of software, that usually means the operations that can be invoked on the box (along with arguments) and in some cases the return types of these operations.

什么它没有做的是它没有定义这些操作的语义是什么,尽管在声明附近(例如,通过注释)记录它们是常见的(并且非常好的实践),或者选择好的命名约定。然而,无法保证会遵循这些意图。

What it does not do is that it does not define what the semantics of these operations are, although it is commonplace (and very good practice) to document them in proximity to the declaration (e.g., via comments), or to pick good naming conventions. Nevertheless, there are no guarantees that these intentions would be followed.

这是一个类比:看电视关闭时。它的界面是它有的按钮,各种插头和屏幕。它的语义和行为是它需要输入(例如,有线节目)并具有输出(在屏幕上显示,声音等)。但是,当您查看未插入的电视时,您将预期的语义投射到界面中。如你所知,当你插上电视时,电视可能会爆炸。但是,根据它的界面,你可以假设它不会制作任何咖啡,因为它没有吸水量。

Here is an analogy: Take a look at your television when it is off. Its interface are the buttons it has, the various plugs, and the screen. Its semantics and behavior are that it takes inputs (e.g., cable programming) and has outputs (display on the screen, sound, etc.). However, when you look at a TV that is not plugged in, you are projecting your expected semantics into an interface. For all you know, the TV could just explode when you plug it in. However, based on its "interface" you can assume that it won't make any coffee since it doesn't have a water intake.

在面向对象编程中,接口通常定义具有该接口的类的实例可以响应的方法(或消息)集。

In object oriented programming, an interface generally defines the set of methods (or messages) that an instance of a class that has that interface could respond to.

令人困惑的是,在某些语言中,如Java,有一个实际的接口,其语言特定的语义。例如,在Java中,它是一组方法声明,没有实现,但接口也对应于一个类型并遵守各种类型规则。

What adds to the confusion is that in some languages, like Java, there is an actual interface with its language specific semantics. In Java, for example, it is a set of method declarations, with no implementation, but an interface also corresponds to a type and obeys various typing rules.

在其他语言中,如C ++,您没有接口。类本身定义了方法,但您可以将类的接口视为非私有方法的声明。由于C ++如何编译,您可以获得头文件,您可以在其中拥有该类的接口而无需实际实现。您还可以使用具有纯虚函数等的抽象类来模仿Java接口。

In other languages, like C++, you do not have interfaces. A class itself defines methods, but you could think of the interface of the class as the declarations of the non-private methods. Because of how C++ compiles, you get header files where you could have the "interface" of the class without actual implementation. You could also mimic Java interfaces with abstract classes with pure virtual functions, etc.

接口绝对不是类的蓝图。根据一个定义,蓝图是详细的行动计划。一个接口承诺一个动作!混淆的原因在于,在大多数语言中,如果您有一个定义一组方法的接口类型,那么实现它的类重复相同的方法(但提供定义),因此接口看起来像一个骨架或一个班级大纲。

An interface is most certainly not a blueprint for a class. A blueprint, by one definition is a "detailed plan of action". An interface promises nothing about an action! The source of the confusion is that in most languages, if you have an interface type that defines a set of methods, the class that implements it "repeats" the same methods (but provides definition), so the interface looks like a skeleton or an outline of the class.

这篇关于“接口”的定义是什么?面向对象编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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