我刚刚在我们的代码库中发现了一个静态嵌套接口。
class Foo {
public static interface Bar {
/* snip */
}
/* snip */
}
我以前从未见过这个。原来的开发人员已经找不到了。因此我不得不问SO:
静态接口背后的语义是什么?如果我移除这些静电,会发生什么变化?为什么会有人这么做?
我刚刚在我们的代码库中发现了一个静态嵌套接口。
class Foo {
public static interface Bar {
/* snip */
}
/* snip */
}
我以前从未见过这个。原来的开发人员已经找不到了。因此我不得不问SO:
静态接口背后的语义是什么?如果我移除这些静电,会发生什么变化?为什么会有人这么做?
当前回答
The question has been answered, but one good reason to use a nested interface is if its function is directly related to the class it is in. A good example of this is a Listener. If you had a class Foo and you wanted other classes to be able to listen for events on it, you could declare an interface named FooListener, which is ok, but it would probably be more clear to declare a nested interface and have those other classes implement Foo.Listener (a nested class Foo.Event isn't bad along with this).
其他回答
The question has been answered, but one good reason to use a nested interface is if its function is directly related to the class it is in. A good example of this is a Listener. If you had a class Foo and you wanted other classes to be able to listen for events on it, you could declare an interface named FooListener, which is ok, but it would probably be more clear to declare a nested interface and have those other classes implement Foo.Listener (a nested class Foo.Event isn't bad along with this).
成员接口是隐式静态的。可以在不改变代码语义的情况下删除示例中的静态修饰符。请参见Java语言规范8.5.1。静态成员类型声明
如果你将类Foo改为接口Foo,上面例子中的“public”关键字也将是多余的,因为
在另一个接口中定义的接口将隐式公开 静态的。
内部接口必须是静态的,才能被访问。该接口与类的实例无关,而是与类本身相关,因此可以通过Foo访问它。酒吧,像这样:
public class Baz implements Foo.Bar {
...
}
在大多数情况下,这与静态内部类没有什么不同。
在Java中,静态接口/类允许接口/类像顶级类一样使用,也就是说,它可以由其他类声明。所以,你可以:
class Bob
{
void FuncA ()
{
Foo.Bar foobar;
}
}
如果没有静态,上述代码将无法编译。这样做的好处是,您不需要一个新的源文件来声明接口。它还可视化地将接口Bar与类Foo关联起来,因为你必须编写Foo。Bar并暗示Foo类对Foo.Bar的实例做一些事情。
Java中类类型的描述。