java - 我可以在方法中有一个接口(interface)吗?

标签 java

我知道 on 可以在类的方法中有一个类声明。 例如。我们可以在方法体中声明用于事件处理的匿名类声明。

但我想知道我能否以同样的方式在类的方法中声明接口(interface)。

那有什么用?

最佳答案

我假设您指的是返回方法的接口(interface)?

简短回答:是的。

为什么?

这是一篇好文章。
Why we return type Mostly Interface rather than Class?

摘录:

The benefit is that returning an interface makes it possible to change the implementation later on. E.g., you might decide after a while that you'd much rather use a LinkedList instead of an ArrayList.....

关于java - 我可以在方法中有一个接口(interface)吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1957157/

相关文章:

java - 手动加密

java - 如何在java/scala中获取系统IOPS和磁盘IOPS

java - 了解媒体扫描仪何时完成工作

java - 是否可以使用 Spring 和 @Value 注释将 YAML 属性读入 Map

java - Jackson无法解析json,返回NPE

java - 如何在android中添加带有边界的彩色折线

java - BigDecimal 类型列的默认值的 JPA 注释

带有最大化按钮的 Java 模态窗口

java - 加载第一行带有标题的 CSV 文件时,有没有办法自动检测字段/列顺序?

java - 将长十六进制编码从 java 移植到 javascript