Generics Kotlin中的泛型问题

Generics Kotlin中的泛型问题,generics,kotlin,Generics,Kotlin,我试图做一个泛型类,但我不能让“funTestInterface”停止要求“什么都不做”,有人知道为什么会发生这种情况吗?谢谢大家! package main interface ITest<S> { fun funTestInterface(param: S): S } class GeneralClass { fun otro(param: ITest<*>, secondParam: Any) { param.funTestInt

我试图做一个泛型类,但我不能让“funTestInterface”停止要求“什么都不做”,有人知道为什么会发生这种情况吗?谢谢大家!

package main

interface ITest<S> {
    fun funTestInterface(param: S): S
}

class GeneralClass {
    fun otro(param: ITest<*>, secondParam: Any) {
        param.funTestInterface(secondParam)
    }
}

class ImplementedClass : ITest<String> {

    override fun funTestInterface(param: String): String {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

}

fun main() {
    val a = GeneralClass()

    a.otro(ImplementedClass(), "")
}
主程序包
接口测试{
趣味趣味界面(参数:S):S
}
类GeneralClass{
fun otro(参数:ITest,第二个参数:任意){
参数funTestInterface(第二个参数)
}
}
类实现类:ITest{
重写funTestInterface(参数:String):String{
TODO(“未实现”)//要更改已创建函数的主体,请使用文件|设置|文件模板。
}
}
主要内容(){
val a=通用类()
a、 otro(实现的类(),“”)
}

一般类应为

class GeneralClass {
fun otro(param: ITest<String>, secondParam: Any) {
    param.funTestInterface(secondParam as String)
}
}
类GeneralClass{
fun otro(参数:ITest,第二个参数:任意){
参数funTestInterface(作为字符串的第二个参数)
}
}
您要在此处使用。您可以将
param
与星形投影一起使用,以仅安全地从中读取值(您可以将其用作输出类型),但在您的情况下,星形被视为输入类型。编译中的星号被视为类型
Nothing
,它不等同于
Any
。也就是说,您不能使用
Any
作为
funTestInterface
的输入

通过泛化
GeneralClass.otro()
函数,您可以简单地解决这个问题:

fun <T> otro(param: ITest<T>, secondParam: T) {
    param.funTestInterface(secondParam)
}
fun-otro(参数:ITest,第二个参数:T){
参数funTestInterface(第二个参数)
}

非常感谢!这就是我需要的!很好的解释!非常感谢。但我需要更一般的东西:D