“对于超类型的Kotlin Android Studio的直接争论,不允许有预测
我在将Java转换为Kotlin时遇到此错误:
Java的
public class HeaderTab extends ExpandableGroup { private String header; public HeaderTab(String title, List items) { super(title, items); } }
科特林
class HeaderTab(title: String, items: List<*>) : ExpandableGroup<*>(title, items) { private val header: String? = null }
Android Studio这样说:
不允许对超类型的直接参数进行投影
我需要在这里修改什么?
使用Any
一个快速修复,或引入一个类型参数,以确保您不打破库的类型安全。
class HeaderTab(title: String, items: List<*>) : ExpandableGroup<Any>(title, items) {
要么
class HeaderTab<E>(title: String, items: List<E>) : ExpandableGroup<E>(title, items) {
问题是kotlin需要完全指定类的类型,所以你可以指定一个特定的类型作为类型参数,或者通过一个新的类型参数。