枚举:每个实例独有的方法

发布于 2024-11-17 21:29:05 字数 598 浏览 1 评论 0原文

另一个问题我了解到,在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

走过海棠暮 2024-11-24 21:29:05

您需要在枚举中声明抽象方法,然后在特定的枚举实例中实现这些方法。

class Outer {

    private enum MyEnum {
        X {
            public void calc(Outer o) {
                // do something
            }
        },
        Y {
            public void calc(Outer o) {
                // do something different
                // this code not necessarily the same as X above
            }
        },
        Z {
            public void calc(Outer o) {
                // do something again different
                // this code not necessarily the same as X or Y above
            }
        };

        // abstract method
        abstract void calc(Outer o);
    }

    public void doCalc() {
        for (MyEnum item : MyEnum.values()) {
            item.calc(this);
        }
    }
}

You need to declare abstract methods in your enum which are then implemented in specific enum instances.

class Outer {

    private enum MyEnum {
        X {
            public void calc(Outer o) {
                // do something
            }
        },
        Y {
            public void calc(Outer o) {
                // do something different
                // this code not necessarily the same as X above
            }
        },
        Z {
            public void calc(Outer o) {
                // do something again different
                // this code not necessarily the same as X or Y above
            }
        };

        // abstract method
        abstract void calc(Outer o);
    }

    public void doCalc() {
        for (MyEnum item : MyEnum.values()) {
            item.calc(this);
        }
    }
}
软糖 2024-11-24 21:29:05

您无法引用这些方法,因为您实际上是为每个枚举创建匿名 (*) 类。由于它是匿名的,因此您只能从匿名类本身或通过反射引用此类方法。

当您在枚举中声明抽象方法并为每个枚举单独实现该方法时,此技术非常有用。

(*) 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."

梨涡少年 2024-11-24 21:29:05

每个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 the enum 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文