java 我们可以在一个接口中定义一个接口吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2332158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can we define an interface within an interface?
提问by giri
I like to know can we define an interface within an interface. like
我想知道我们可以在一个接口中定义一个接口。喜欢
interface abc {
void show();
public interface xyz {
void read();
}
}
This was question asked in interview. Any real time use of this.
这是面试时问的问题。任何实时使用。
回答by HotTester
Yes, we can do it. The definition of the nested interface in java is as follows:
是的,我们可以做到。java中嵌套接口的定义如下:
A nested interface is any interface whose declaration occurs within the body of another class or interface. A top-level interface is an interface that is not a nested interface.
嵌套接口是其声明出现在另一个类或接口的主体内的任何接口。顶级接口是不是嵌套接口的接口。
Refer thisfor more.
有关更多信息,请参阅此内容。
Further ...
更远 ...
One reason could be that the outer interface has a method that takes a callback implementation as an argument. The nested interface is, in that case, the contract that the callback method must implement. I don't see a reason to declare that callback interface at the top level.
一个原因可能是外部接口有一个将回调实现作为参数的方法。在这种情况下,嵌套接口是回调方法必须实现的契约。我看不出有理由在顶层声明回调接口。
public interface Processor {
void execute(NotificationListener listener);
interface NotificationListener {
void processingCompleted();
}
}
Another good reading at sun site about this topic is here
在太阳网站上关于这个话题的另一个很好的阅读是在这里
In particular, notice that when you implement an interface, you are not required to implement any interfaces nested within.
特别要注意的是,当你实现一个接口时,你不需要实现任何嵌套在其中的接口。
回答by joe
Sure.. Look at SOURCE CODE for java.util.Map interface. Map interface contains a nested Entry interface.
当然.. 查看 java.util.Map 接口的源代码。Map 接口包含一个嵌套的 Entry 接口。
Interestingly, in the source code it simply says
有趣的是,在源代码中它只是说
interface Entry <K,V> {
..
}
but the javadoc says
但是 javadoc 说
public static interface Map.Entry<K,V>
I guess this is because nested interfaces are implicitly "public static" even though the source code doesn't say that. (But methods inside an interface are implicity public, and cannot be static,that is, only instance methods are permitted in interfaces).
我想这是因为即使源代码没有这么说,嵌套接口也是隐含的“公共静态”。(但是接口内部的方法是隐式公开的,不能是静态的,即接口中只允许实例方法)。
-dbednar 2013-07-02
-dbednar 2013-07-02
回答by user207421
Yes.
是的。
You could have tested that for yourself and got a completely definitive, opinion-free, risk-free answer in about 30 seconds.
您可以自己测试一下,并在大约 30 秒内得到一个完全确定、没有意见、没有风险的答案。
Waiting possibly forever for a possibly incorrect response on a forum is by comparison not a rational mode of enquiry.
相比之下,可能永远等待论坛上可能不正确的回复并不是一种理性的查询方式。
回答by kyle87
We are using it in our application, Interface inside interface, using this basically for being functionality specific constants, so that accidentally no other will create new constants somewhere else in the project, related to this Service1.
我们在我们的应用程序中使用它,Interface inside interface,使用它基本上是作为功能特定的常量,所以无意中没有其他人会在项目中的其他地方创建与此 Service1 相关的新常量。
example Code:
示例代码:
Public interface Service1{
public interface ServiceInter1{
public Interface In{
Declare your own constants
}
public Interface Out{
Declare your own constants
}
}
}
回答by user4733704
yes, we can define.
是的,我们可以定义。
Inside Map interface define Entry as like below. public interface Map {
在 Map 界面中,Entry 定义如下。公共接口映射{
/**
* Map.Entry is a key/value mapping which is contained in a Map.
*/
public static interface Entry<K, V> {
.....some loigic
}
}
回答by Vinny
The real use of this is to simplify a complex system by providing a Facade interface. - Check for the Facade design pattern.
它的真正用途是通过提供 Facade 接口来简化复杂的系统。- 检查外观设计模式。

