简体   繁体   English

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

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

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 . 我不明白为什么我们可以从变量a访问DOG,CAT或FISH常量。 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); 我想(并且也在书中写道)DOG,FISH,CAT是常量,以类似于public static final Animals DOG = new Animals(1);的方式实现public static final Animals DOG = new Animals(1); So if they really are static why can we access them from a ? 所以,如果他们真的是静态的,为什么我们可以从a The last line is the way I am familiar with. 最后一行是我熟悉的方式。

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 DOG on it. 上面所做的是声明枚举类型的对象,并在其上引用静态DOG The compiler knows the type of a , and knows that you want Animal.DOG . 编译器知道的类型a ,并且知道你想要Animal.DOG But this kills readability. 但这会杀死可读性。

I believe the purpose of this is to shorten the usage of enums. 我相信这样做的目的是缩短枚举的用法。 a.DOG instead of Animal.DOG . a.DOG而不是Animal.DOG If you really want to shorten it, you can use import static fqn.of.Animal and then use simply DOG . 如果你真的想缩短它,你可以使用import static fqn.of.Animal然后使用DOG

Writing a.DOG is the same as writing Animal.DOG . 编写a.DOG与编写Animal.DOG相同。 That is, the compiler will replace the variable with its compile time type Animal. 也就是说,编译器将使用其编译时类型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

You can access 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. 在您的示例中,BTW a为空。 Does it get initialized somewhere? 它是否在某处初始化?

EDIT 编辑

I know the compiler knows what a.DOG is bound to, as statics can't be overridden. 我知道编译器知道a.DOG绑定了什么,因为静态不能被覆盖。 It does not need a to determine the call, only the compile-time type of a , which it has. 它并不需要一个确定的号召,只有编译时类型 ,由它。

I also know that the example works even though a is null (I tried so I know:). 我也知道这个例子可以正常工作,即使anull (我试过,所以我知道:)。

But I still think it's weird you can get stuff from null . 但我仍然认为你可以从null得到东西很奇怪。 And it's confusing: 这令人困惑:

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

When I would be debugging the NPE I would assume that a can't be null as the println worked fine. 当我调试NPE时,我会假设a不能为null,因为println工作正常。 Confusing. 混乱。

Ah well, Java. 好吧,Java。 If you think you've seen it all you get something else again :) 如果你认为你已经看到了所有你再次得到别的东西:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM