Kotlin-分类的亚元素
我正在尝试实现我可以在整个程序中使用的角色类/界面/枚举。我希望角色有些分类 - 我希望某些角色是A型,某些角色是B型,并且某些角色是多种类型的一部分。我希望能够知道每种类型的所有角色 - 因此,枚举/密封的类结构就是这个想法。 我尝试了以下实现 -
sealed interface UserRole {
val roleName: String
}
enum class RoleA(override val roleName: String): UserRole {
A(roleName = "A"),
B(roleName = "B"),
C(roleName = "C"),
D(roleName = "D");
companion object {
fun fromRoleName(roleName: String): RoleA? =
values().singleOrNull { it.roleName == roleName }
}
}
enum class RoleB(override val roleName: String, val secondParam: String): UserRole {
A(roleName = "A", secondParam = "A"),
E(roleName = "E", secondParam = "E"),
F(roleName = "F", secondParam = "F");
companion object {
fun fromRoleName(roleName: String): RoleB? =
values().singleOrNull { it.roleName == roleName }
}
}
如您所见,A是两个枚举的一部分,但理想情况下,我希望它们成为相同的对象。同样,如果我需要更多类型的角色,我希望将来能够创建更多这些枚举。
在尝试密封接口之前,我只有1个大枚举称为userrole,它只有所有值,然后我使用了另一个称为roletype的类,并且在两者之间进行了简单的映射来获得我想要的东西,但我认为它并不是这样做的我想要的。谁能提出一种更好的方法来分类枚举价值?
I am trying to implement a Role class/interface/enum that I can use throughout my program. I want the roles to be somewhat categorized - I want some roles to be of type A, some roles to be of type B, and some roles to be part of multiple types. I want to be able to know all the roles from each type - so an enum/sealed class structure is the idea.
I tried the following implementation -
sealed interface UserRole {
val roleName: String
}
enum class RoleA(override val roleName: String): UserRole {
A(roleName = "A"),
B(roleName = "B"),
C(roleName = "C"),
D(roleName = "D");
companion object {
fun fromRoleName(roleName: String): RoleA? =
values().singleOrNull { it.roleName == roleName }
}
}
enum class RoleB(override val roleName: String, val secondParam: String): UserRole {
A(roleName = "A", secondParam = "A"),
E(roleName = "E", secondParam = "E"),
F(roleName = "F", secondParam = "F");
companion object {
fun fromRoleName(roleName: String): RoleB? =
values().singleOrNull { it.roleName == roleName }
}
}
As you can see, A is part of both enums, but I would ideally want them to be the same object. Likewise I want to have the ability to create more of these enums in the future in case I need more types of roles.
Before I tried sealed interfaces, I simply had 1 big enum called UserRole that simply had all the values, and I used another class called RoleType and a simple mapping between the two to get what I wanted, but I don't think it does exactly what I want. Can anyone suggest a better way to categorize enum values?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过定义这样的每个角色类型的密封接口来反映您在类型系统中的角色:
然后您的
角色
类可以定义为seale>密封类< /code>和每个班级都可以将您的角色类型作为合适。
此 do 在定义每个角色所需的代码中带来了一些开销,因此在权衡每个变体的优点时要注意这一点。
You could reflect your roles in the type system by defining a sealed interface for each role type like this:
Then your
Role
class could be defined as asealed class
and every class could implement your Role types as fit.This does bring some overhead in the amount of code necessary to define each Role, so be aware of this when weighing pros and cons of each variant.