在《算法》中的散列表一节,在用拉链法实现散列表的API时要求实现以下一个方法:

public Iterable<Key> keys()

我们知道Iterable是一个接口,那么一个方法怎么会返回一个接口呢?在《Effective Java》中第52条为“通过接口引用对象”

as parameter types. More generally, you should favor the use of interfaces rather than classes to refer to objects. If appropriate interface types exist, then parameters, return values, variables, and fields should all be declared using interface types. The only time you really need to refer to an object’s class is when you’re creating it with a constructor. To make this concrete, consider the case of Vector, which is an implementation of the List interface. Get in the habit of typing this:

// Good - uses interface as type
List<Subscriber> subscribers = new Vector<Subscriber>(); rather than this: // Bad - uses class as type!
Vector<Subscriber> subscribers = new Vector<Subscriber>(); If you get into the habit of using interfaces as types, your program will be much more flexible. If you decide that you want to switch implementations, all you have to do is change the class name in the constructor (or use a different static factory). For example, the first declaration could be changed to read List<Subscriber> subscribers = new ArrayList<Subscriber>(); and all of the surrounding code would continue to work. The surrounding code was unaware of the old implementation type, so it would be oblivious to the change. There is one caveat: if the original implementation offered some special functionality not required by the general contract of the interface and the code depended on that functionality, then it is critical that the new implementation provide the same functionality. It is entirely appropriate to refer to an object by a class rather than an interface if no appropriate interface exists. For example, consider value classes, such as String and BigInteger. Value classes are rarely written with multiple implementations in mind. They are often final and rarely have corresponding interfaces. It is perfectly appropriate to use such a value class as a
parameter, variable, field, or return type. More generally, if a concrete class has no associated interface, then you have no choice but to refer to it by its class whether or not it represents a value. The Random class falls into this category. A second case in which there is no appropriate interface type is that of objects belonging to a framework whose fundamental types are classes rather than interfaces. If an object belongs to such a class-based framework, it is preferable to refer to it by the relevant base class, which is typically abstract, rather than by its implementation class. The java.util.TimerTask class falls into this category. A final case in which there is no appropriate interface type is that of classes that implement an interface but provide extra methods not found in the interface— for example, LinkedHashMap. Such a class should be used to refer to its instances only if the program relies on the extra methods. It should rarely be used as a parameter type (Item 40). These cases are not meant to be exhaustive but merely to convey the flavor of situations where it is appropriate to refer to an object by its class. In practice, it should be apparent whether a given object has an appropriate interface. If it does, your program will be more flexible if you use the interface to refer to the object; if not, just use the least specific class in the class hierarchy that provides the required functionality. Reference: Effective Java 2nd Edition by Joshua Bloch

子类是可以向上转型为父类,因此传递给父类的消息子类是都可以接受的,接口和其实现类有点这样的关系,当然还是有不同。当我们返回一个接口类型时,其实就是意味着,以后你完全可以把其返回值赋给一个实现了此接口的实现类的实例。这样编写的好处是,此接口的实现类的实例在运行时可以任意灵活指定,而不需要修改接口函数的代码。如果有合适的接口类型存在,那么对于参数,返回值,变量和域来说,都应该使用接口类型进行声明。如果没有合适的接口存在,完全可以用类而不是接口来引用对象。

查看JDK的Collections相关的库可以发现大量使用了返回接口来引用对象,例如iterator()这些方法的实现都是返回一个interface,实际只需要实现这个interface即可,对于不同的集合实现interator不一样并不影响iterator的调用。

回到最开始public Iterable<Key> keys()该如何实现,这里采用ArrayList来实现,当然也可以返回其他任何实现了Iterable接口的类:

public Iterable<Key> keys() {
ArrayList<Key> ka = new ArrayList<Key>();
for (int i = 0; i < M; i++) {
ka.addAll(st[i].keys());
}
return ka;
}

Java中接口作为方法的返回的更多相关文章

  1. java中的compareto方法以及LIst列表排序的详细介绍【转】

    java中的compareto方法的详细介绍 javacompareTo  java中的compareto方法,返回参与比较的前后两个字符串的asc码的差值,看下面一组代码 String a=&quo ...

  2. java中的compareto方法的详细介绍

    java中的compareto方法的详细介绍 Java Comparator接口实例讲解(抽象方法.常用静态/默认方法) 一.java中的compareto方法 1.返回参与比较的前后两个字符串的as ...

  3. Java中的native方法

    博客引用地址:Java中的native方法 今天花了两个小时把一份关于什么是Native Method的英文文章好好了读了一遍,以下是我依据原文的理解. 一. 什么是Native Method 简单地 ...

  4. 详解Java中的clone方法

    详解Java中的clone方法 参考:http://blog.csdn.net/zhangjg_blog/article/details/18369201/ 所谓的复制对象,首先要分配一个和源对象同样 ...

  5. Java中接口是否可以继承多个接口?

    可以. 接口是常量值和方法定义的集合.接口是一种特殊的抽象类. java类是单继承的.classB Extends classA java接口可以多继承.Interface3 Extends Inte ...

  6. 浅谈Java中的hashcode方法

    哈希表这个数据结构想必大多数人都不陌生,而且在很多地方都会利用到hash表来提高查找效率.在Java的Object类中有一个方法: 1 public native int hashCode(); 根据 ...

  7. 千万不要误用 java 中的 HashCode 方法

    刚才debug追堆栈的时候发现一个很奇怪的问题 我用IE8和Google的浏览器访问同一个地址 Action的 scope="session" 也设置了 而且两个浏览器提交的参数m ...

  8. Java中的toString()方法

    Java中的toString()方法 目录 Java中的toString()方法 1.    对象的toString方法 2.    基本类型的toString方法 3.    数组的toString ...

  9. Java中的main()方法详解

    在Java中,main()方法是Java应用程序的入口方法,也就是说,程序在运行的时候,第一个执行的方法就是main()方法,这个方法和其他的方法有很大的不同,比如方法的名字必须是main,方法必须是 ...

随机推荐

  1. addEventListener,attachEvent

    addEventListener是js填加事件:用法如下: target.addEventListener(type,listener,useCapture) target: 文档节点.documen ...

  2. JavaScript开发者常忽略或误用的七个基础知识点

    JavaScript 本身可以算是一门简单的语言,但我们也不断用智慧和灵活的模式来改进它.昨天我们将这些模式应用到了 JavaScript 框架中,今天这些框架又驱动了我们的 Web 应用程序.很多新 ...

  3. js-- 一些题目

    1. ~~3.14~~3.14=-((~3.14)+1)=-(-(3.14+1)+1)=-(-(3+1)+1)=-(-4+1) =-(-3)=3 按位非(NOT)(~)操作数的负值减1. 2. var ...

  4. ubuntu14.04LS中安装SSH

    我只能说: 蛋疼了 因为 1.曾经12.04和13.10的源已经不能使用了(PS毕竟支持的时间到了) 2网上有好多说是更新源的 , 打开etc/...文件 ,然后粘贴一下他们给的源的地址 或许有些是可 ...

  5. Jenkins简单使用介绍

    一.Jenkins的配置 1. 下载地址: Jenkins的官方网站:http://jenkins-ci.org/ 目前最新版本的Windows版:http://mirror.xmission.com ...

  6. 【转】Nginx+php-fpm+MySQL分离部署详解

    转:http://www.linuxidc.com/Linux/2015-07/120580.htm Nginx+php-fpm+MySQL分离部署详解 [日期:2015-07-26] 来源:Linu ...

  7. matlab c# 混合编程

    MWArray错误: matlab 64位 vs 32位 1. visual studio没有专门的64位版.但32位版可以在64位系统上面正常使用.2.安装VS2010的时候,在安装选项里面,选择了 ...

  8. [转]RAID技术介绍和总结

    以下内容转自伯乐在线:http://blog.jobbole.com/83808/ 原文出处: 涯余(@若东临于沧海) ---------------------------------------- ...

  9. PDF 补丁丁 0.4.1.839 测试版发布:调整页面留白

    新的测试版的补丁功能实现了调节页面留白的功能(之前的820版尚未实现该功能),页面合并功能支持从资源管理器拖放文件或目录到列表,还修正了一些问题. 欢迎下载测试.

  10. HDU 5773 The All-purpose Zero 求LIS

    求最长上升子序列长度: 单纯的dp时间复杂度是O(n*n)的 dp[i] = max(dp[j]+1); (0=<j<=i-1 && a[i]>a[j]) 用二分可以 ...