gpt4 book ai didi

java - 这个未定义的方法/集合在这个静态初始化器中做了什么?

转载 作者:行者123 更新时间:2023-11-29 07:43:11 24 4
gpt4 key购买 nike

我正在努力理解 ZXing's CharacterSetECI.java 是什么在它的静态初始化器中做

具体来说,我不知道这是怎么可能的:

private static final Map<Integer,CharacterSetECI> VALUE_TO_ECI = new HashMap<>();
private static final Map<String,CharacterSetECI> NAME_TO_ECI = new HashMap<>();

static {
for (CharacterSetECI eci : values()) {
for (int value : eci.values) {
VALUE_TO_ECI.put(value, eci);
}
NAME_TO_ECI.put(eci.name(), eci);
for (String name : eci.otherEncodingNames) {
NAME_TO_ECI.put(name, eci);
}
}
}

private final int[] values;

注意在第一个 foreach 中使用了 values(),方法 values() 没有在类中的任何地方定义。最接近它的是定义在它下面的 int array values,但我想我们可以同意它绝对不包含 CharacterSetECI 类型的对象。

当我试图在一个新类中只键入这段代码时(创建一个构造函数、最终实例变量数组和静态初始值设定项),Eclipse 提示 values() 未定义,但是当我将整个代码复制到一个新类,Eclipse 只是提示一些类型无法解析,但 values() 得到了通过。我尝试按住 ctrl 并单击 values() 以查看它引用的内容,但它不可单击。

注意:

如果您不信任链接,这个类可以在 ZXing 的 3.1.0 版源代码中找到,可以在 mavencentral 的 com.google.zxing.common 包中找到。

最佳答案

每个 enum 都有一个隐式的 values 方法;来自 JLS §8.9.3 :

The members of an enum type E are all of the following:

  • ...

  • The following implicitly declared methods:

    /**
    * Returns an array containing the constants of this enum
    * type, in the order they're declared. This method may be
    * used to iterate over the constants as follows:
    *
    * for(E c : E.values())
    * System.out.println(c);
    *
    * @return an array containing the constants of this enum
    * type, in the order they're declared
    */
    public static E[] values();

关于java - 这个未定义的方法/集合在这个静态初始化器中做了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28148109/

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