Java接口是否有真正的意义? [英] Is there any true point to the Java interface?

查看:42
本文介绍了Java接口是否有真正的意义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
实际上如何使用Java接口?

我不是从学术流行语的角度讲,而是从实用的开发者的角度讲.

I'm not talking from an accademic buzzword point of view but from a pratical developer point of view.

举个例子:-

    Class1 implements Interface 
 public String methodOne() {
  return "This is Class1.methodOne()";
 }

 public String methodTwo() {
  return "This is Class1.methodTwo()";
 }
    }
Class2:

    Class2 implements Interface 
 public String methodOne() {
  return "This is Class2.methodOne()";
 }

 public String methodTwo() {
  return "This is Class2.methodTwo()";
 }
    }

使用界面:-

 Client {
  Interface intface = new Class1();

  intface.methodOne();
  intface.methodTwo();

  Interface intface = new Class2();
  intface.methodOne();
  intface.methodTwo();
}

但是,只写文字有什么好处:-

But what are the benefits over just writting:-

 Client {
Class1 clas1 = new Class1();

clas1.methodOne();
clas1.methodTwo();

Class2 clas2 = new Class2();
clas2.methodOne();
clas2.methodTwo();
 }

并完全绕过接口.

为了增加代码层,接口似乎只是另外的代码层,还是除了您正在访问的类拥有的方法在这里"之外,还有更多给他们?

Interfaces just seem to be an additional layer of code for the sake of an additional layer of code, or is there more to them than just "Here are the methods that the class you are accessing has"?

推荐答案

使用独立类时,不需要接口.但是,当您具有类型层次结构时,接口确实是必不可少的.

When using standalone classes, you don't need interfaces. However, when you have a type hierarchy, interfaces are indeed indispensable.

您的简单示例并没有真正做到公正,但是让我们将您的界面重命名为更有用和更具体的内容,例如 Sorter 算法,该算法可以获取项目列表并对其进行排序.您可以实现多种不同的排序算法,并且您可能希望根据上下文更改所使用的算法(即,对于大型数据集,QuickSort更快,而对于小型数据集,BubbleSort更好.等等)想要将客户端代码绑定到一种特定的算法.通过使用多态的 Sorter 类型(实现为接口),您可以将不同的具体分类器对象传递给客户端,而无需知道(并关心)其实际使用的算法.而且,您可以随时引入一种更好的排序算法,或者删除一种效率低下的排序算法,而无需客户注意任何事情.

Your simple example does not really do justice, but let's rename your interface to something more useful and concrete, e.g. a Sorter algorithm which can take a list of items and sort them. You can have several different sorting algorithms implemented, and you may want to change the algorithm used, based on the context (i.e. QuickSort is faster for large data sets, but BubbleSort is better for small data sets, etc.) So you don't want to tie the client code to one specific algorithm. By using the polymorphic Sorter type (implemented as an interface), you can pass different concrete sorter objects to the client, without it knowing (and caring) what algorithm it is actually using. And you can any time introduce a better sorting algorithm, or remove one which proved inefficient, without the clients noticing anything.

没有接口,这样的壮举将是不可能的.另一种选择是直接从if-else或switch块中的所有位置(可能重复)直接调用选择的排序方法,或者在添加/删除排序算法时忘记正确更新所有位置时不可避免地引入错误...更不用说每次更改后您都需要重新编译所有客户端代码:-(

Such a feat would be impossible without interfaces. The alternative would be calling the sort method of choice directly from (possibly duplicated) if-else or switch blocks all over the place, with the inevitable possibility of introducing bugs when forgetting to update all places properly when a sorting algorithm is added/removed... not to mention that you would need to recompile all client code after each such change :-(

这篇关于Java接口是否有真正的意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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