如何让java允许像Map.Entry这样的名字?我以为不允许有点

时间:2016-07-07 12:45:35

标签: java hashmap

此接口名称中的点是如何允许的,我无法创建名称中带有点的另一个类/接口名称。

3 个答案:

答案 0 :(得分:5)

因为EntryMap界面中的嵌套界面。如果需要,您可以创建类似的东西:

class MyClass {
  static class Nested { }
  public static void main(String[] args) {
    MyClass.Nested n = new MyClass.Nested();
  }
}

或者更直接地回答您的问题:

class MyClass {
  static class MyEntry implements Map.Entry { }
  public static void main(String[] args) {
    Map.Entry n = new MyEntry();
  }
  interface Map {
    interface Entry {}
  }
}

答案 1 :(得分:1)

正如Kevin Esche评论的那样,在创建内部类时,它的完全限定名称将变为foo.bar.Outer.Inner

答案 2 :(得分:1)

Entry接口在Map接口中声明。它是一个内部接口。因此,为了使用它,我们必须引用Map类,因此引用Map.Entry。

相关问题