抽象java枚举 [英] abstract java enum
问题描述
我写了一个应该依赖于枚举的库,但是实际的枚举应该由我的库的用户定义.
I write a library that should rely on enums but the actual enum should be defined by the user of my library.
在下面的示例中, authorize
方法需要枚举类型为 Permission
的参数.
In the following example the authorize
method requires parameters of the enum type Permission
.
acl.authorize(userX, Permission.READ, Permission.WRITE)
我的图书馆应该能够处理图书馆用户定义的任意权限.但是如果没有 Permission
枚举,我将无法编译我的库.所以我需要类似的东西
My library should be able to handle arbitrary permissions defined by the library user. But I cannot compile my library without a Permission
enum. So I would need something like
abstract enum Permission
在我的图书馆中.有解决方法吗?
in my library. Is there a workaround to do this?
推荐答案
我将使用枚举将实现的接口.类似于
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
[...]
然后您的API将使用 PermissionType
类型
Then your API would accept parameters using the PermissionType
type
这篇关于抽象java枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!