枚举:每个实例独有的方法
从另一个问题我了解到,在Java中可以为每个实例定义特定方法枚举:
public class AClass {
private enum MyEnum{
A { public String method1(){ return null; } },
B { public Object method2(String s){ return null; } },
C { public void method3(){ return null; } } ;
}
...
}
我很惊讶这甚至是可能的,这个特定于每个实例的“专有方法”是否有一个名称来查找文档?
另外,应该如何使用?因为接下来没有编译:
private void myMethod () {
MyEnum.A.method1();
}
我应该如何使用这些“专有”方法?
From another question I have learnt that it is possible in Java to define specific methods for each one of the instances of an Enum:
public class AClass {
private enum MyEnum{
A { public String method1(){ return null; } },
B { public Object method2(String s){ return null; } },
C { public void method3(){ return null; } } ;
}
...
}
I was surprised that this is even possible, do this "exclusive methods" specific to each instance have a name to look for documentation?
Also, how is it supposed to be used? Because the next is not compiling:
private void myMethod () {
MyEnum.A.method1();
}
How am I supposed to use these "exclusive" methods?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要在枚举中声明抽象方法,然后在特定的枚举实例中实现这些方法。
You need to declare abstract methods in your enum which are then implemented in specific enum instances.
您无法引用这些方法,因为您实际上是为每个枚举创建匿名 (*) 类。由于它是匿名的,因此您只能从匿名类本身或通过反射引用此类方法。
当您在枚举中声明抽象方法并为每个枚举单独实现该方法时,此技术非常有用。
(*) JLS 8.9 枚举部分说:“枚举常量的可选类主体隐式定义了一个匿名类声明(第 15.9.5 节),该声明扩展了直接封闭的枚举类型。”
You cannot refer to those methods, because you are effectively creating anonymous (*) class for each enum. As it is anonymous, you can reference such methods only from inside your anonymous class itself or through reflection.
This technique is mostly useful when you declare abstract method in your enumeration, and implement that method for each enum individually.
(*) JLS 8.9 Enums part says: "The optional class body of an enum constant implicitly defines an anonymous class declaration (§15.9.5) that extends the immediately enclosing enum type."
每个
enum
都是一个匿名内部类。因此,与任何匿名内部类一样,您可以添加所需的所有方法,但无法在类外部引用它们,因为该类没有定义方法的类型。在
enum
实现上允许方法的优点是它允许策略模式,其中枚举本身具有抽象方法或默认实现,以及enum
的特定成员> 该方法的实现可能会做一些不同的事情。我使用这种技术大大降低了 switch 语句的代码复杂性。无需在其他类中打开枚举,只需获取对它的引用并调用方法,然后让枚举本身来处理它。当然,这取决于具体情况是否有意义,但它可以极大地降低代码复杂性。
Each
enum
is an anonymous inner class. So like any anonymous inner class, you can add all of the methods you want, but there is no way to reference them outside of the class, as the class doesn't have a type that defines the methods.The advantage of allowing methods on the
enum
implementation is that it allows for a strategy pattern, where the enum itself has an abstract method or a default implementation, and specific members of theenum
have implementations of that method that may do something different.I have used this technique to greatly reduce code complexity on switch statements. Instead of switching on the enum in some other class, just get a reference to it and call a method, and let the enum itself take care of it. Of course that depends on the scenario if it makes sense, but it can reduce code complexity tremendously.