引用通用类型T的实例

时间:2019-06-12 16:39:54

标签: android oop kotlin generic-programming

当Animal类覆盖Zoo类时,是否可以返回泛型T的实例?

classInstance<T>() returns a type mismatch; required class<T> found T.

    private inline fun <reified Eat : T> classInstance(): Eat = Eat::class.java.newInstance()


    abstract class Animal<Result> {

        abstract fun bark(): Class<Result>

    }

    open class Zoo<T>(protected var listener: RequestListener<T>?) : Callback<T>, Animal<T>() {

        override fun bark(): Class<T> = classInstance<T>()
    }    

bark()函数将用于以下目的:

Gson().fromJson(inputStreamReaderResult, bark()),其中bark()必须是Class类型

0 个答案:

没有答案