是否可以在 Java 枚举中实现某种形式的命名空间?

发布于 2024-11-30 23:43:40 字数 927 浏览 1 评论 0原文

在下面的枚举中,我看到 Message 枚举中存在三个不同的消息类:FormSiteAdmin.

是否可以在枚举中实现一种命名空间机制,这样,

Message.SITE_ERROR 
Message.ADMIN_ERROR

您就可以编写这样的内容:

Message.Site.ERROR 
Message.Admin.ERROR

以便SiteAdmin代表“命名空间”,下面可以存在哪些附加消息类别?

public enum Message {
    //FORM
    FORM_EMPTY("You've gotta put something in the form."),

    //SITE
    SITE_ERROR("Whoa. What happened?"),
    SITE_ALERT("Hey, it's that time again.");

    //ADMIN
    ADMIN_ERROR("Gotta look into this, dude."),
    ADMIN_ALERT("Time to get the lead out.");


    private String messageString;
    private Message(String messageString){
        this.messageString=messageString;
    }

    @Override
    public String toString() { 
        return messageString; 
    }   
}

In the enum below I see three three distinct classes of messages that exist within the Message enum: Form, Site and Admin.

Is it possible to implement a type of namespace mechanism in an enum so that, instead of writing

Message.SITE_ERROR 
Message.ADMIN_ERROR

you write, this:

Message.Site.ERROR 
Message.Admin.ERROR

so that Site and Admin represent the "namespace" below which additional categories of messages can exist?

public enum Message {
    //FORM
    FORM_EMPTY("You've gotta put something in the form."),

    //SITE
    SITE_ERROR("Whoa. What happened?"),
    SITE_ALERT("Hey, it's that time again.");

    //ADMIN
    ADMIN_ERROR("Gotta look into this, dude."),
    ADMIN_ALERT("Time to get the lead out.");


    private String messageString;
    private Message(String messageString){
        this.messageString=messageString;
    }

    @Override
    public String toString() { 
        return messageString; 
    }   
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

混浊又暗下来 2024-12-07 23:43:40

可以用包吗?

例如:

com.foo.messages

是您的基础包。然后你可以从那个

com.foo.messages.site
com.foo.messages.admin

等等扩展......

You could use packages?

for example:

com.foo.messages

is your base package. Then you can expand from that

com.foo.messages.site
com.foo.messages.admin

Et cetera...

静待花开 2024-12-07 23:43:40

您可以将 Message 设为声明枚举 SiteAdmin 的接口。但是,当然,您不能传递 Message 对象并期望一个枚举,而只是一个 Message. 对象。

你要这个做什么?

You could make Message an interface that declares enums Site and Admin. But then, of course, you can't pass around a Message object and expect an enum, just a Message.<something> object.

What do you want this for?

空心空情空意 2024-12-07 23:43:40

当然,使用内部类就可以了。

public interface State {
    String getMessageString();
}

public class Message {

    public static enum Form implements State {    
        EMPTY("You've gotta put something in the form."),

        private final String msg;
        public Form(String msg) { this.msg = msg; }
        public String getMessageString() { return msg; }
    }

    public static enum Site implements State {
        ERROR("Whoa. What happened?"),
        ALERT("Hey, it's that time again.");

        private final String msg;
        public Site(String msg) { this.msg = msg; }
        public String getMessageString() { return msg; }
    }

    public static enum Admin implements State {
        ADMIN_ERROR("Gotta look into this, dude."),
        ADMIN_ALERT("Time to get the lead out.");

        private final String msg;
        public Admin(String msg) { this.msg = msg; }
        public String getMessageString() { return msg; }
    }
}

我认为您可能会摆脱具有公共基类的重复代码,但我的记忆在扩展枚举方面很模糊,因此这可能不起作用......

Of course, just use inner classes.

public interface State {
    String getMessageString();
}

public class Message {

    public static enum Form implements State {    
        EMPTY("You've gotta put something in the form."),

        private final String msg;
        public Form(String msg) { this.msg = msg; }
        public String getMessageString() { return msg; }
    }

    public static enum Site implements State {
        ERROR("Whoa. What happened?"),
        ALERT("Hey, it's that time again.");

        private final String msg;
        public Site(String msg) { this.msg = msg; }
        public String getMessageString() { return msg; }
    }

    public static enum Admin implements State {
        ADMIN_ERROR("Gotta look into this, dude."),
        ADMIN_ALERT("Time to get the lead out.");

        private final String msg;
        public Admin(String msg) { this.msg = msg; }
        public String getMessageString() { return msg; }
    }
}

I think you may get rid of the repeated code with a common base class but my memory is fuzzy on extending enums so that may not work...

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