简体   繁体   English

这个 Java 类的 Kotlin 等价物是什么

[英]What is the Kotlin equivalent of this Java class

I am trying to convert the following Java class in Kotlin我正在尝试在 Kotlin 中转换以下 Java 类

abstract class BaseExpandedViewCreator implements NotificationViewCreator
{
   protected  RawNotification rawNotification;
   protected final Context context;

   BaseExpandedViewCreator(@NonNull Context context)
   {
       this.context = Objects.requireNonNull(context);
   }

  @Override
  public void setRawNotification(@NonNull RawNotification rawNotification)
  {
      this.rawNotification = rawNotification;
      initRawNotification(rawNotification);
  }

 /**
  * Override this function to initialise {@link RawNotification} for view creators if needed.
  */
 protected void initRawNotification(@NonNull RawNotification rawNotification) {}
 }

Kotlin interface Kotlin 接口

interface NotificationViewCreator {

   fun setRawNotification(rawNotification: RawNotification)
}

This is my implementation:这是我的实现:

    abstract class BaseExpandedViewCreator(
        protected val context: Context
    ):NotificationViewCreator {

    var rawNotification: RawNotification ? = null

    fun setRawNotification(rawNotification: RawNotification) {
        this.rawNotification = rawNotification
        initRawNotification(rawNotification)
    }
     fun initRawNotification(rawNotification: RawNotification) {}
   }

I get the following error我收到以下错误

Platform declaration clash: The following declarations have the same JVM signature (setRawNotification(Lcom/myproject/RawNotification;)V): 
public final fun <set-rawNotification>(<set-?>: RawNotification): Unit defined in com.myproject.BaseExpandedViewCreator
public final fun setRawNotification(rawNotification: RawNotification): Unit defined in com.myproject.BaseExpandedViewCreator

You can change visibility of var rawNotification to private to avoid property/setter name clash:您可以将var rawNotification可见性更改为private以避免属性/设置器名称冲突:

abstract class BaseExpandedViewCreator(
        private val context: Context
): NotificationViewCreator {

    private lateinit var rawNotification: RawNotification // if you want non-nullable property
    // OR
    private var rawNotification: RawNotification? = null // if you are OK with nullable property

    override fun setRawNotification(rawNotification: RawNotification) {
        this.rawNotification = rawNotification
        initRawNotification(rawNotification)
    }

    fun initRawNotification(rawNotification: RawNotification) {}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM