为什么isInstanceOf[T]
方法不能按预期运行?
在下面,我定义了一个hello
类和伴随对象。在hello对象中,我在代码“ this.isInstanceOf[T]
”行中测试了hel.typetest[Int]
,当类型true
为T
时,Int
为何呢? / p>
object hello {
def main(args: Array[String]): Unit = {
Console.println("main")
val hel = new hello
hel.typetest[Int]
}
}
class hello {
def typetest[T: ClassTag]: Unit = {
Console.println(this.isInstanceOf[T])
Console.println(this.getclass)
}
}
输出:
main
true
class hello