我刚刚在我们的代码库中发现了一个静态嵌套接口。

class Foo {
    public static interface Bar {
        /* snip */
    }
    /* snip */
}

我以前从未见过这个。原来的开发人员已经找不到了。因此我不得不问SO:

静态接口背后的语义是什么?如果我移除这些静电,会发生什么变化?为什么会有人这么做?


当前回答

Jesse's answer is close, but I think that there is a better code to demonstrate why an inner interface may be useful. Look at the code below before you read on. Can you find why the inner interface is useful? The answer is that class DoSomethingAlready can be instantiated with any class that implements A and C; not just the concrete class Zoo. Of course, this can be achieved even if AC is not inner, but imagine concatenating longer names (not just A and C), and doing this for other combinations (say, A and B, C and B, etc.) and you easily see how things go out of control. Not to mention that people reviewing your source tree will be overwhelmed by interfaces that are meaningful only in one class.So to summarize, an inner interface enables the construction of custom types and improves their encapsulation.

class ConcreteA implements A {
 :
}

class ConcreteB implements B {
 :
}

class ConcreteC implements C {
 :
}

class Zoo implements A, C {
 :
}

class DoSomethingAlready {
  interface AC extends A, C { }

  private final AC ac;

  DoSomethingAlready(AC ac) {
    this.ac = ac;
  }
}

其他回答

内部接口必须是静态的,才能被访问。该接口与类的实例无关,而是与类本身相关,因此可以通过Foo访问它。酒吧,像这样:

public class Baz implements Foo.Bar {
   ...
}

在大多数情况下,这与静态内部类没有什么不同。

要非常直接地回答你的问题,请查看Map.Entry。

地图。条目

这也可能是有用的

静态嵌套Inerfaces博客条目

成员接口是隐式静态的。可以在不改变代码语义的情况下删除示例中的静态修饰符。请参见Java语言规范8.5.1。静态成员类型声明

Typically I see static inner classes. Static inner classes cannot reference the containing classes wherease non-static classes can. Unless you're running into some package collisions (there already is an interface called Bar in the same package as Foo) I think I'd make it it's own file. It could also be a design decision to enforce the logical connection between Foo and Bar. Perhaps the author intended Bar to only be used with Foo (though a static inner interface won't enforce this, just a logical connection)

1998年,Philip Wadler提出了静态接口和非静态接口之间的区别。

在我看来,唯一的区别是 接口非静态是指它现在可以包含非静态的内部 类;所以这个改变不会使任何现有的Java无效 项目。

例如,他提出了“表达问题”的解决方案,即“你的语言能表达多少”的表达与“你试图用你的语言表达的术语”的表达之间的不匹配。

静态和非静态嵌套接口之间的区别可以在他的示例代码中看到:

// This code does NOT compile
class LangF<This extends LangF<This>> {
    interface Visitor<R> {
        public R forNum(int n);
    }

    interface Exp {
        // since Exp is non-static, it can refer to the type bound to This
        public <R> R visit(This.Visitor<R> v);
    }
}

他的建议从未出现在Java 1.5.0中。因此,所有其他答案都是正确的:静态和非静态嵌套接口没有区别。