【发布时间】:2019-03-09 01:50:56
【问题描述】:
我有一个接受Class<GenericType<Constraint>> 的函数。
当我传递该GenericType<Constraint> 的子类时,编译器错误并显示以下消息:
Type Inference Failed. Expected Type Mismatch
但是,如果我将类型转换为它的超类型,它运行良好(带有警告)。如何在不强制转换的情况下做到这一点?
open class Bag<T>
class IntBag: Bag<Int>()
fun testStuff(type: Class<Bag<Int>>) {
// Do stuff
}
testStuff(IntBag::class.java) // This won't compile
testStuff(IntBag::class.java as Class<Bag<Int>>) // This compiles with a warning, but runs fine
【问题讨论】:
标签: java generics kotlin covariance