有一个java类,使用androidStudio翻译成kotlin。出现错误,不知道如何正确翻译。
的java代码:
public class BaseDataImpl extends BaseData {
private final BaseData[] translators;
public BaseDataImpl(final BaseData... translators) {
this.translators = cloneArray(translators);
}
public static <T> T[] cloneArray(final T[] array) {
if (array == null) {
return null;
}
return array.clone();
}
}
代码翻译后,得到错误:required Array<BaseData>?, found Array<out BaseData>
,但调用translators
中cloneArray<BaseData>(translators)
的定义为 val translators: Array<BaseData>?
,
谁能帮忙解释一下?
class BaseDataImpl(vararg translators: BaseData) : BaseData() {
private val translators: Array<BaseData>?
init {
this.translators = cloneArray<BaseData>(translators) //<=== error: required Array<BaseData>?, found Array<out BaseData>
}
companion object {
fun <T> cloneArray(array: Array<T>?): Array<T>? {
return array?.clone()
}
}
}