Kotlin-分类的亚元素

发布于 2025-02-10 21:27:51 字数 1034 浏览 2 评论 0原文

我正在尝试实现我可以在整个程序中使用的角色类/界面/枚举。我希望角色有些分类 - 我希望某些角色是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 技术交流群。

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

发布评论

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

评论(1

路弥 2025-02-17 21:27:52

可以通过定义这样的每个角色类型的密封接口来反映您在类型系统中的角色:

sealed interface Type1 {
    val t: Int
    fun test()
}

sealed interface Type2 {
    val s: String
}

然后您的角色类可以定义为seale>密封类< /code>和每个班级都可以将您的角色类型作为合适。

sealed class Role

class A(override val t: Int, override val s: String) : Role(), Type1, Type2 {
    override fun test() {
        print("hello")
    }
}

class B(override val s: String) : Role(), Type2

do 在定义每个角色所需的代码中带来了一些开销,因此在权衡每个变体的优点时要注意这一点。

You could reflect your roles in the type system by defining a sealed interface for each role type like this:

sealed interface Type1 {
    val t: Int
    fun test()
}

sealed interface Type2 {
    val s: String
}

Then your Role class could be defined as a sealed class and every class could implement your Role types as fit.

sealed class Role

class A(override val t: Int, override val s: String) : Role(), Type1, Type2 {
    override fun test() {
        print("hello")
    }
}

class B(override val s: String) : Role(), Type2

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.

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