Java:访问枚举(enum)中的常量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5206603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 09:57:34  来源:igfitidea点击:

Java: access to the constants in an enumeration (enum)

javaenumsconstants

提问by florian

reading the SCJP book, I've found something like this in the chapter 1 "self-test" :

阅读 SCJP 书,我在第 1 章“自测”中发现了类似的内容:

enum Animals {
    DOG("woof"), CAT("meow"), FISH("burble");
    String sound;
    Animals(String s) { sound = s; }
}

class TestEnum {      
    static Animals a; 
    public static void main(String[] args) {                                                                                     
        System.out.println(a.DOG.sound + " " + a.FISH.sound);   

        // the following line is from me
        System.out.println(Animals.DOG.sound + " " + Animals.FISH.sound);
    }
} 

Note: the code compile fine. What I don't understand is why we can access the DOG, CAT or FISH constants from the variable a. I thought (and it is also written in the book) that the DOG, FISH, CAT being constants are implemented in a way similar to public static final Animals DOG = new Animals(1);So if they really are static why can we access them from a? The last line is the way I am familiar with.

注意:代码编译正常。我不明白的是为什么我们可以从变量中访问 DOG、CAT 或 FISH 常量a。我认为(并且这本书也写了)DOG、FISH、CAT 是常量的实现方式类似于public static final Animals DOG = new Animals(1);所以如果它们真的是静态的,为什么我们可以从a?最后一行是我熟悉的方式。

采纳答案by josefx

Writing a.DOGis the same as writing Animal.DOG. That is, the compiler will replace the variable with its compile time type Animal. It is considered bad code since it hides the fact that it relies on the compile time type instead of the dynamic type of a.

写作 a.DOG与写作相同Animal.DOG。也就是说,编译器将用其编译时类型 Animal 替换该变量。它被认为是糟糕的代码,因为它隐藏了它依赖编译时类型而不是动态类型的事实a

回答by Bozho

Although this works, don't do it like that. Use enums with Animal.DOG, Animal.CAT, etc.

虽然这有效,但不要那样做。使用与枚举Animal.DOGAnimal.CAT等等。

What the above does is declare an object of the enum type, and reference the static DOGon it. The compiler knows the type of a, and knows that you want Animal.DOG. But this kills readability.

上面所做的是声明一个枚举类型的对象,并DOG在其上引用静态。编译器知道 的类型a,并且知道您想要的Animal.DOG。但这会降低可读性。

I believe the purpose of this is to shorten the usage of enums. a.DOGinstead of Animal.DOG. If you really want to shorten it, you can use import static fqn.of.Animaland then use simply DOG.

我相信这样做的目的是缩短枚举的使用。a.DOG而不是Animal.DOG. 如果你真的想缩短它,你可以使用import static fqn.of.Animal然后简单地使用DOG.

回答by extraneon

You canaccess statics from an instance, but it's really bad taste as statics aren't as much bound to an instance as bound to the class.

可以从实例访问静态,但这真的很糟糕,因为静态绑定到实例的程度不如绑定到类。

Whatever the book says, don't use statics that way. And if you run checkstyle and the like, they warn about it too :)

无论书中怎么说,都不要那样使用静力学。如果你运行 checkstyle 之类的,他们也会发出警告:)

BTW a is null in your example. Does it get initialized somewhere?

顺便说一句,在您的示例中, a 为空。它是否在某处初始化?

EDIT

编辑

I know the compiler knows what a.DOG is bound to, as statics can't be overridden. It does not need ato determine the call, only the compile-time type of a, which it has.

我知道编译器知道 a.DOG 绑定到什么,因为无法覆盖静态。它并不需要一个确定的号召,只有编译时类型的一个,其有。

I also know that the example works even though ais null(I tried so I know:).

我也知道即使a,这个例子也能工作(我试过所以我知道:)。

But I still think it's weird you can get stuff from null. And it's confusing:

但我仍然认为你可以从null获取东西很奇怪。它令人困惑:

Animals a = null;
System.out.println(a.DOG); // OK
a.doSomething(); // NullPointerException

When I would be debugging the NPE I would assume that acan't be null as the printlnworked fine. Confusing.

当我调试 NPE 时,我会假设a不能为空,因为println工作正常。令人困惑。

Ah well, Java. If you think you've seen it all you get something else again :)

嗯,爪哇。如果你认为你已经看过这一切,你又会得到别的东西:)