What is the Scala equivalent of Java's ClassName.class?

后端 未结 1 396
走了就别回头了
走了就别回头了 2020-12-08 03:56

How do I get an instance of Class in Scala? In Java, I can do this:

Class stringClass = String.class;

What would

相关标签:
1条回答
  • 2020-12-08 04:23

    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
    
    0 讨论(0)
提交回复
热议问题