gpt4 book ai didi

java - Java 中接口(interface)名称 Map.Entry 中的点 '.'

转载 作者:行者123 更新时间:2023-12-01 07:18:47 24 4
gpt4 key购买 nike

据我所知,不能有“.”。 java中的类或接口(interface)名称。但是,Map.Entry 接口(interface)似乎不遵循该规则。请纠正我的理解或“.”背后的原因。

最佳答案

阅读此内容:

https://docs.oracle.com/javase/8/docs/api/java/util/Map.html?is-external=true

条目嵌套在Map中,您可以使用点('.')来访问它。

也请阅读此内容:https://dzone.com/articles/what-inner-interface-java

引自dzone(第二个链接):

Inner interface is also called nested interface, which means declare an interface inside of another interface. For example, the Entry interface is declared in the Map interface.

public interface Map {
interface Entry{
int getKey();
}

void clear();
}

Why Use Inner Interface?

There are several compelling reasons for using inner interface:

  • It is a way of logically grouping interfaces that are only used in one place.

  • It increases encapsulation.

  • Nested interfaces can lead to more readable and maintainable code.

One example of inner interface used in java standard library is java.util.Map and Java.util.Map.Entry. Here java.util.Map is used also as a namespace. Entry does not belong to the global scope, which means there are many other entities that are Entries and are not necessary Map’s entries. This indicates that Entry represents entries related to the Map.

关于java - Java 中接口(interface)名称 Map.Entry 中的点 '.',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48071074/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com