Java——如何设计自己的类型?

发布于 2024-09-06 07:44:42 字数 109 浏览 2 评论 0原文

是否可以设计自己的 Java 类型,类似于可扩展的enum

例如,我有某个模块使用的用户角色,然后子包提供其他角色。

JDK 方面会涉及什么?

Is it possible to design your own Java Type, similar to an extensible enum?

For instance, I have user roles that a certain module uses and then a sub-package provides additional roles.

What would be involved on the JDK side of things?

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

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

发布评论

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

评论(5

送你一个梦 2024-09-13 07:44:42

由于枚举无法扩展,因此您必须伪造它。

创建一个带有受保护构造函数的类。

然后,您可以在类中创建 public static final FakeEnum 实例。

public class FakeEnum {

    private String name;
    private Object something;

    protected FakeEnum(String name, Object otherParam) {
        this.name = name;
        this.something = otherParam;
    }

    // public getters

    public static final FakeEnum ONE = new FakeEnum("one", null);
    public static final FakeEnum TWO = new FakeEnum("two", null);
    public static final FakeEnum THRE = new FakeEnum("thre", null);
}

然后你可以扩展它并添加更多东西,如下所示:

public class ExtendedFakeEnum extends FakeEnum {

    public static final FakeEnum EXTENDED_ONE = new FakeEnum("extended_one", null);
    public static final FakeEnum EXTENDED_TWO = new FakeEnum("extended_two", null);

}

Since Enums can't be extended, you have to fake it.

Make a class with a protected constructor.

Then you can create public static final FakeEnum instances in your class.

public class FakeEnum {

    private String name;
    private Object something;

    protected FakeEnum(String name, Object otherParam) {
        this.name = name;
        this.something = otherParam;
    }

    // public getters

    public static final FakeEnum ONE = new FakeEnum("one", null);
    public static final FakeEnum TWO = new FakeEnum("two", null);
    public static final FakeEnum THRE = new FakeEnum("thre", null);
}

And then you can extend it and add some more things to it like so:

public class ExtendedFakeEnum extends FakeEnum {

    public static final FakeEnum EXTENDED_ONE = new FakeEnum("extended_one", null);
    public static final FakeEnum EXTENDED_TWO = new FakeEnum("extended_two", null);

}
香草可樂 2024-09-13 07:44:42

好的,

我要做的就是编写一个接口,然后编写几个实现,以了解如何在特定事件中查找要通知的用户。正确的实现将在运行时注入,然后它将做任何需要做的事情来找到正确的用户。该实现可以简单地采用参数来配置要查找的组名称,然后返回用户列表。

我正在学习更多地使用界面/合同设计。我过去的大部分开发都只有一个实现,所以我认为这是一个没有实际意义的问题,并忘记了该工具/手段。

谢谢,

沃尔特

Ok,

What I will do is write an interface and then several implementations for how to find users to notify in a particular event. The correct implementation will get injected at run-time and then it will do whatever it needs to do to find the correct users. That implementation may simply take arguments to configure the group name to look for and then return a list of users.

I am learning to use interfaces / design by contract more. Most of my development in the past has only ever had a single implementation so I saw this as a moot point and forgot about that tool / means.

Thanks,

Walter

最舍不得你 2024-09-13 07:44:42

可扩展枚举的概念没有意义。枚举用于静态声明为其自己的类型创建的整个实例集。允许延长期限将无法确保这一点。

The concept of an extensible enum doesn't make sense. An enum is used to declare statically the entire set of instances that will ever be made for its own type. Allowing it to be extended would make that impossible to ensure.

一百个冬季 2024-09-13 07:44:42

在 Java 中设计自己的类型是不可能的。您需要做的任何事情都可以使用各种设计模式来完成。

Designing your own type in Java is impossible. Anything you need to do can be done using various design patterns.

满意归宿 2024-09-13 07:44:42

如果您需要一个“可扩展枚举”,那么字典可能更适合您,请查看 java.util.Dictionary,其中 K 是键名(您将如何引用特定值,V 是该键应该返回的值/对象,

我认为这是我所见过的最接近可扩展枚举的值。

另外,请查看在这个问题上,这也可能解决它。

If you need an "extensible enum" it might be that a dictionary would suit you better, look at java.util.Dictionary<K,V> where K is the keyname (how you would refer to the particular value, and V is the value/object that should be returned by said Key.

I think thats the closest I've ever come to an extensible Enum.

Also, have a look at this question on SO, this might solve it too.

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