我一直在查看TypeTag及其同类的文档,但未能解决此问题。
在下面的例子中,是否有一种方法可以通过替换来解释运行时的参数化类型T???带有Scala的魔力,所以运行时会打印"SomeClass"?
class SomeClass
trait TheTrait[T] {
def showClassNameOfT = println("class name of T is: " + ???)
}
object Foo extends TheTrait[SomeClass] {
def main(args: Array[String]): Unit = {
showClassNameOfT
}
}
向方法中添加一个隐式ClassTag[T]
参数:
trait TheTrait[T] {
def showClassNameOfT(implicit ct:ClassTag[T]) =
println("class name of T is: " + ct.runtimeClass.getSimpleName)
}