抽象 java 枚举

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2251344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 20:18:36  来源:igfitidea点击:

abstract java enum

javaenumsabstract-class

提问by deamon

I write a library that should rely on enums but the actual enum should be defined by the user of my library.

我写了一个应该依赖枚举的库,但实际的枚举应该由我的库的用户定义。

In the following example the authorizemethod requires parameters of the enum type Permission.

在以下示例中,该authorize方法需要枚举类型的参数Permission

acl.authorize(userX, Permission.READ, Permission.WRITE)

My library should be able to handle arbitrary permissions defined by the library user. But I cannot compile my library without a Permissionenum. So I would need something like

我的图书馆应该能够处理图书馆用户定义的任意权限。但是我不能在没有Permission枚举的情况下编译我的库。所以我需要类似的东西

abstract enum Permission

in my library. Is there a workaround to do this?

在我的图书馆。有没有办法做到这一点?

回答by Steen

I would use an interface which the enum would then implement. Something along the lines of

我将使用一个接口,然后枚举将实现该接口。类似的东西

public interface PermissionType{}

which would be used by e.g. the client to define an enum such as

例如,客户端将使用它来定义枚举,例如

public enum Permission implements PermissionType
[...]

Then your API would accept parameters using the PermissionTypetype

然后您的 API 将接受使用PermissionType类型的参数

回答by Bozho

Here are the steps I'd suggest.

这是我建议的步骤。

  1. write an annotation - public @interface Permission
  2. make the user annotate each of his permission enums with that annotation:

    @Permission
    public enum ConcretePermissionEnum {..}
    
  3. Make your authorizemethod look like:

    public boolean authorize(User user, Enum... permissions) {
        for (Enum permission : permissions) {
           if (permission.getClass().isAnnotationPresent(Permission.class)){
              // handle the permission
           }
        }
    }
    
  1. 写注释—— public @interface Permission
  2. 让用户使用该注释来注释他的每个权限枚举:

    @Permission
    public enum ConcretePermissionEnum {..}
    
  3. 让你的authorize方法看起来像:

    public boolean authorize(User user, Enum... permissions) {
        for (Enum permission : permissions) {
           if (permission.getClass().isAnnotationPresent(Permission.class)){
              // handle the permission
           }
        }
    }
    


If you want your Permission enums to have some specific methods, or just want a 'marker', then you can make the user enums implement an interface of yours (instead of being annotated):

如果你想让你的 Permission 枚举有一些特定的方法,或者只是想要一个“标记”,那么你可以让用户枚举实现你的接口(而不是被注释):

interface PermissionInterface {..}
enum ConcretePermission implements PermissionInterface

This will enable a compile-time, rather than a run-time check, as with the annotation approach, with the authorizemethod signature looking like:

这将启用编译时检查,而不是运行时检查,与注释方法一样,authorize方法签名如下所示:

public boolean authorize(User user, PermissionInterface... permissions)