Scala 等价于 Java 的 ClassName.class 是什么?

如何在 Scala 中获得 Class的实例:

Class<String> stringClass = String.class;

在 Scala 中等价于什么?

17468 次浏览

There is a method classOf in scala.Predef that retrieves the runtime representation of a class type.

val stringClass = classOf[String]

You can use the getClass method to get the class object of an instance at runtime in the same manner as Java

scala> val s = "hello world"
s: String = hello world


scala> s.getClass
res0: Class[_ <: String] = class java.lang.String