Java中的接口如何工作? [英] How does an Interface in Java work?

查看:126
本文介绍了Java中的接口如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是自学Java,而且我一直在关于接口的章节。我根本无法理解它们是如何在Java中工作的。

I'm self learning Java, and I'm stuck on a chapter about Interfaces. I simply cannot understand how they work in Java.

我相信我完全理解界面的含义以及它们在日常情况和技术中的应用方式。

I believe I understand perfectly what Interface means and how they apply in everyday situations and technology.

但是当谈到Java时,代码方面和逻辑方面,我陷入困境。我不明白。这个概念是如何运作的?

But when it comes to Java, code-wise and logic-wise, I'm stuck. I don't get it. How does the concept work?

假设我有3个对象和1个接口对象。 2对象是ObjectCalculatingA,ObjectCalculatingB,ObjectMathFunctions和ObjectInterface。

Say I have 3 objects, and 1 interface object. 2 Objects are ObjectCalculatingA, ObjectCalculatingB, ObjectMathFunctions, and ObjectInterface.

据说在ObjectInterface中必须有对ObjectMathFunctions的某种引用,因此ObjectCalculatingA和B只能访问ObjectMathFunctions中的数学函数没有在A和B中再次写入它们。

Supposedly in ObjectInterface there must be some sort of reference to the ObjectMathFunctions, so that ObjectCalculatingA and B can just access the math functions in ObjectMathFunctions without writting them all again in A and B.

我是对的吗?

推荐答案

存在一个界面以便于多态性。它允许声明一个合同,任何实现接口的类必须遵守。因此,通过查找 事物之间的通用性 ,可以实现 抽象 和模型复杂性。

An interface exists to facilitate polymorphism. It allows declaring a contract that any class that implements the interface must honor. And so it is a way to achieve abstraction and model complexity by looking for commonality between things.

一个例子?形状怎么样?所有形状都有一个区域,对吗?所以你可以拥有以下课程:

An example? How about shapes? All shapes have an area, right? So you could have the following classes:


  • Square

  • Circle

然后假设您有另一个允许您收集形状的类,并返回总面积:

Then let's say you have another class that allows you to collect shapes, and return the total area:

for (Shape shape in shapes)
{
    area += shape.area()  //This is polymorphism
}

在上面的示例中,我们不关心形状是方形还是圆形。我们也可以接受。我们只关心它实现 Shape 接口。每个对象都将提供他们自己的区域自定义实现 - 这些内部细节并不重要,只是它尊重 区域合同 。现在看看我们如何管理复杂性?我们可以使用这个课程,而不必担心内部发生的所有事情。此时对我们来说非常重要,而不是 它是如何实现的,这让我们 专注于手头的问题 不会因复杂的细节而分心。

In the example above, we don't care whether the shape is a square or a circle. We can accept either. We only care that it implements the Shape interface. Each object will provide their own custom implementation of area - these internal details aren't important, only that it honors the area contract . See now how we're managing complexity? We can use the class without having to worry about all of the things that go on inside. At this point what it does is important to us, not how it does it, which lets us focus on the problem at hand not getting distracted by complex details.

这种多态性是面向对象编程被认为是编程中如此强大的进化步骤的原因之一。面向对象编程中的其他关键基础概念是:

This polymorphism is one of the reasons why object oriented programming was considered such a powerful evolutionary step in programming. The other key foundation concepts in Object Oriented Programming are:

  • Encapsulation
  • Inheritance

。 。 。你还需要学习这些。

. . . you'll also need to learn these.

抽象基类与接口

正如评论所说,另一种方式实现多态是使用Abstract Base Class。你应该选择哪个?

As a comment said, another way to achieve polymorphism is to use an Abstract Base Class. Which should you choose?


  • 使用实现它的接口类将拥有自己的层次结构和依赖项。例如媒体播放器。电影播放器​​和声音播放器可能具有完全不同的基类,因此请使用界面。

  • Use an interface class implementing it will have its own hierarchy and dependencies. For example a media player. A Movie Player and a Sound Player might have totally different base classes, so use an interface.

当事物之间存在某些共性时,请使用Abstract基类,但细节会有所不同。例如,消息解析框架。

Use an Abstract base class when you have some commonality between things, but the specifics vary. For example a message parsing framework.

这篇关于Java中的接口如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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