[英]What is the Kotlin equivalent of this Java class
我正在嘗試在 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 NotificationViewCreator {
fun setRawNotification(rawNotification: RawNotification)
}
這是我的實現:
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) {}
}
我收到以下錯誤
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
您可以將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.