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

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

问题描述

好的,我的一个朋友反复讨论接口"在编程中的含义.

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 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天全站免登陆