用多个扩展密封替换枚举 类
replace enums with multiple extends sealed classes
目前我有三个枚举 classes 表示我的状态机中的状态,一个用于显示操作
interface State
enum class OperationState : State {
InProgress,
Finished,
Error
}
enum class FirstState : State {
//some states
}
enum class NextState: State {
//some states
}
enum class LastState: State {
//some states
}
在我的服务中,我有:
when (state) {
is FirstState -> {
//do something
changeFirstStateOperationState(state)
}
is NextState -> {
//do something
changeNextStateOperationState(state)
}
is LastState -> {
//do something
changeLastStateOperationState(state)
}
}
private fun changeFirstStateOperationState(state: FirstState){
when(state){
FirstState.A -> OperationState.Error
listOf(FirstState.B, FirstState.C) -> OperationState.InProgress
FirstState.D -> OperationState.Finished
}
}
我想将我当前的实现更改为密封的 classes。
我试过类似的东西:
sealed class State {
sealed class OperationState : State() {
sealed class FirstState : OperationState() {
object A: FirstState()
object B: FirstState()
object C: FirstState()
object D: FirstState()
}
sealed class NextState:OperationState(){ ... }
sealed class LastState:OperationState(){ ... }
}
}
但是没用...
有没有什么方法可以使用密封的 class 来知道当前的 OperationStatus 而无需将 State 映射到它?
sealed class State {
sealed class OperationState : State() {
sealed class FirstState : OperationState() {
object A: FirstState()
object B: FirstState()
object C: FirstState()
object D: FirstState()
}
sealed class NextState:OperationState(){ }
sealed class LastState:OperationState(){ }
}
}
fun main() {
checkState(State.OperationState.FirstState.A)
}
fun checkState(state: State) {
when(state) {
is State.OperationState.FirstState.A -> "it's A"
is State.OperationState.FirstState -> "it's another FirstState"
is State.OperationState.NextState -> "it's a NextState"
is State.OperationState.LastState -> "LastState"
}.run(::println)
}
请注意,您必须指定整个类型名称 (State.OperationState...
),除非您进行导入(如果嵌套这么多,可能会推荐),而且您还必须使用 is
在 when
匹配器中,因为您正在比较类型。第一个不需要它 (FirstState.A
),因为你是在与特定的单例对象进行比较,所以它的外观取决于你自己
目前我有三个枚举 classes 表示我的状态机中的状态,一个用于显示操作
interface State
enum class OperationState : State {
InProgress,
Finished,
Error
}
enum class FirstState : State {
//some states
}
enum class NextState: State {
//some states
}
enum class LastState: State {
//some states
}
在我的服务中,我有:
when (state) {
is FirstState -> {
//do something
changeFirstStateOperationState(state)
}
is NextState -> {
//do something
changeNextStateOperationState(state)
}
is LastState -> {
//do something
changeLastStateOperationState(state)
}
}
private fun changeFirstStateOperationState(state: FirstState){
when(state){
FirstState.A -> OperationState.Error
listOf(FirstState.B, FirstState.C) -> OperationState.InProgress
FirstState.D -> OperationState.Finished
}
}
我想将我当前的实现更改为密封的 classes。 我试过类似的东西:
sealed class State {
sealed class OperationState : State() {
sealed class FirstState : OperationState() {
object A: FirstState()
object B: FirstState()
object C: FirstState()
object D: FirstState()
}
sealed class NextState:OperationState(){ ... }
sealed class LastState:OperationState(){ ... }
}
}
但是没用...
有没有什么方法可以使用密封的 class 来知道当前的 OperationStatus 而无需将 State 映射到它?
sealed class State {
sealed class OperationState : State() {
sealed class FirstState : OperationState() {
object A: FirstState()
object B: FirstState()
object C: FirstState()
object D: FirstState()
}
sealed class NextState:OperationState(){ }
sealed class LastState:OperationState(){ }
}
}
fun main() {
checkState(State.OperationState.FirstState.A)
}
fun checkState(state: State) {
when(state) {
is State.OperationState.FirstState.A -> "it's A"
is State.OperationState.FirstState -> "it's another FirstState"
is State.OperationState.NextState -> "it's a NextState"
is State.OperationState.LastState -> "LastState"
}.run(::println)
}
请注意,您必须指定整个类型名称 (State.OperationState...
),除非您进行导入(如果嵌套这么多,可能会推荐),而且您还必须使用 is
在 when
匹配器中,因为您正在比较类型。第一个不需要它 (FirstState.A
),因为你是在与特定的单例对象进行比较,所以它的外观取决于你自己