【问题标题】:Accessing the nested level of sealed class in android kotlin访问android kotlin中密封类的嵌套级别
【发布时间】:2021-11-23 15:23:46
【问题描述】:

我有一个密封类

MyEvents.kt

sealed class MyEvents<out T : Any>{
    sealed class HelloBroadcasting<out T : Any> : MyEvents<Nothing>() {
        sealed class FeatureSegments : HelloBroadcasting<Nothing>() {
            class SegmentationCourseSeekChapterChange(val context: Context) : FeatureSegments()
            class SegmentationChapterNameClicked(val context: Context, chapterName: String) : FeatureSegments()
            class SegmentationChapterSelectionFromChapterList(val context: Context) : FeatureSegments()
        }
    }
}

我叫密封类

sendTheEventEvent(MyEvents.HelloBroadcasting.FeatureSegments.SegmentationChapterNameClicked(mContext,it.textDirection.toString()))

我正在尝试将事件接收为

fun sendCleverTapEvent(event: MyEvents<Int>) {
        when(event){
            is MyEvents.HelloBroadcasting.FeatureSegments.SegmentationChapterNameClicked -> test(event.context,event.) // ---> Here I am not able to access the name
            is MyEvents.HelloBroadcasting.FeatureSegments.SegmentationChapterSelectionFromChapterList -> TODO()
            is MyEvents.HelloBroadcasting.FeatureSegments.SegmentationCourseSeekChapterChange -> TODO()
        }
    }

我无法访问接收部分中的名称。嵌套级别存在时如何正确执行此操作?

【问题讨论】:

    标签: android kotlin sealed-class


    【解决方案1】:

    您必须将chapterName 设为属性:

    - chapterName: String
    + val chapterName: String
    

    【讨论】:

    • 该死,我错过了...谢谢....我会标记答案已接受:)
    猜你喜欢
    • 2019-05-15
    • 1970-01-01
    • 2021-11-05
    • 1970-01-01
    • 2021-11-06
    • 1970-01-01
    • 1970-01-01
    • 2015-07-16
    • 1970-01-01
    相关资源
    最近更新 更多