简体   繁体   English

枚举,静态方法和实现接口方法的用途是什么?

[英]Enums, What's the use of static methods and implementing interface methods?

I am having some small doubts regarding Enums . 我对Enums有一些小疑问。 I have the following confusions :- 我有以下困惑:

  1. What is the use of static methods in Enums ? Enumsstatic方法的用途是什么? Mostly we use Enums for declaring constants. 通常,我们使用Enums声明常量。 What is the use of this feature? 此功能有什么用?
  2. I see, an Enum can implement an interface. 我知道, Enum可以实现一个接口。 What would be the use? 有什么用? I am just confused about practical life usage. 我只是对实际生活的使用感到困惑。
  3. Another confusion is, see the code snippet below, 另一个困惑是,请参见下面的代码片段,

     enum Position { FIRST, SECOND, THIRD; public static void main(String[] args) { //line 1 Position p = Position.SECOND; //line 2 switch(Position.THIRD) { //line 3 case FIRST: //line 4 System.out.println("Congrats, You are the winner");//line 5 break; //line 6 case SECOND : //line 7 System.out.println("Congrats, You are the runner");//line 8 break; //line 9 default : //line 10 System.out.println("Better luck next time"); //line 11 } } } 

    If use in case statement like Position . 如果使用case语句,例如Position First,it gives a compile time error. 首先,它给出了编译时错误。 I understand JLS does not allow this. 我了解JLS不允许这样做。 Reason written is with a class format, it can't make a symbolic linkage. 编写的原因具有类格式,不能建立符号链接。 But reverse is true. 但是相反是正确的。 Like in line-3, i am using the enum itself with fully qualified name. 像第3行一样,我使用具有完全限定名称的enum本身。 So, my point is, what is the meaning of symbolic linkage and why line-3 is allowed also? 因此,我的意思是,符号链接的含义是什么?为什么也允许使用第3行?

Note It was basically a typo mistake. 注意这基本上是一个错字错误。 My main question was, we are using syntax Position.THIRD in switch condition(which is allowed by compiler) but when we use same syntax in case, it is not allowed. 我的主要问题是,我们在切换条件中使用了Position.THIRD语法(编译器允许),但是如果我们使用相同的语法以防万一,则不允许这样做。 Why is that? 这是为什么?

Quote From JLS 来自JLS的报价

(One reason for requiring inlining of constants is that switch statements require constants on each case, and no two such constant values may be the same. The compiler checks for duplicate constant values in a switch statement at compile time; the class file format does not do symbolic linkage of case values.) (要求内联常量的一个原因是switch语句在每种情况下都需要常量,并且两个这样的常量值可能都不相同。编译器在编译时检查switch语句中是否有重复的常量值;类文件格式不进行案例值的符号链接。)

Here it mentions about Symbolic linkage. 这里提到了符号链接。 But when i wrote something like this in switch condition switch(Position.THIRD) , it was allowed where as in CASE statement it was. 但是,当我在开关条件switch(Position.THIRD)编写类似这样的内容时,它被允许在CASE语句中的位置。

What is the use of static methods in Enums? 枚举中静态方法的用途是什么?

Same as in any other type. 与任何其他类型相同。 For enums, they're often "given this string value, return the equivalent enum" or something similar. 对于枚举,它们通常是“赋予此字符串值,返回等效的枚举”或类似的名称。 (In cases where valueOf isn't appropriate, eg where there are multiple representations.) (在valueOf不合适的情况下,例如,有多个表示形式。)

I see, an Enum can implement an interface. 我知道,枚举可以实现一个接口。 What would be the use? 有什么用?

Same as with any interface - to decouple implementation from usage. 与任何接口相同-使实现与使用脱钩。 For example, I've made enums implement a localization-related interface before now. 例如,我之前使枚举实现了与本地化相关的接口。

My main question was, we are using syntax Position.THIRD in switch condition(which is allowed by compiler) but when we use same syntax in case, it is not allowed. 我的主要问题是,我们在切换条件中使用了Position.THIRD语法(编译器允许),但是如果我们使用相同的语法以防万一,则不允许这样做。 Why is that? 这是为什么?

It's just a language decision - the enum type has to be the same as the type of the switch value itself, so it's redundant information. 这只是一种语言决策-枚举类型必须与开关值本身的类型相同,因此它是冗余信息。 It has nothing to do with the section of the JLS you quoted. 它与您引用的JLS部分无关。

枚举可以使单身人士变得非常漂亮……基本上可以回答您所有的问题:)

Enums are a lot like normal classes, except they have certain restrictions on instantiation and they can't extend classes, etc. 枚举与普通类非常相似,不同之处在于枚举对实例化有一定的限制,并且不能扩展类等。

Static methods are typically factory methods that return instances of the enum, often looking up the instance whose field matches a parameter. 静态方法通常是返回枚举实例的工厂方法,通常会查询其字段与参数匹配的实例。 See this example . 请参阅此示例

Enums can have fields, getters and setters and other methods and can thus implement interfaces - including generic ones. 枚举可以具有字段,获取器和设置器以及其他方法,因此可以实现接口-包括通用接口。 See this example . 请参阅此示例

You use an enum instead of a class when you want a finite set if named instances. 如果需要命名实例的有限集,可以使用枚举而不是类。

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

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