[英]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.