首页 > 解决方案 > 下面 kotlin 代码中第一行的含义是什么。从 Java 到 Kotlin 的 Newbee

问题描述

我是一名 Java 程序员,也是 Kotlin 的新手。请帮助我理解下面的代码,尤其是第一行。

class SiteListEventBus : EventBus<SiteListEventBus.SiteListChangeEvent, String, NotificationHandler<SiteListEventBus.SiteListChangeEvent>>() {

    data class SiteListChangeEvent(val entityId: String, val routingKey: String)

    override fun getSubscriptionKey(event: SiteListChangeEvent?): String {
        return event!!.routingKey
    }
}

标签: kotlin

解决方案


这是我阅读第一行的方式:

  1. class SiteListEventBus
    定义一个新类。
  2. : FooBar()
    FooBar使用空构造函数扩展类。
  3. FooBar实际上EventBus<SiteListEventBus.SiteListChangeEvent, String, NotificationHandler<SiteListEventBus.SiteListChangeEvent>>
    ,泛型在这里的应用方式与您在 Java 中所期望的方式相同。
  4. class SiteListEventBus : FooBar() {
    开始实现SiteListEventBus类。

以下是我阅读其余部分的方式:

  1. data class SiteListChangeEvent(val entityId: String, val routingKey: String)
    创建一个data class.
  2. override fun getSubscriptionKey
    与注解override类似。@Override覆盖方法getSubscriptionKey
  3. event!!.routingKey
    event变量可以为空。我建议阅读有关!!operator的内容。

推荐阅读