我知道 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/