本文介绍了Java 的 ClassName.class 的 Scala 等价物是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在 Scala 中获取 Class
的实例?在 Java 中,我可以这样做:
How do I get an instance of Class
in Scala? In Java, I can do this:
Class<String> stringClass = String.class;
Scala 中的等价物是什么?
What would be the equivalent in Scala?
推荐答案
有一个方法 classOf 在 scala.Predef
中,用于检索类类型的运行时表示.
There is a method classOf in scala.Predef
that retrieves the runtime representation of a class type.
val stringClass = classOf[String]
您可以使用getClass
方法在运行时获取实例的类对象,方式与Java相同
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
这篇关于Java 的 ClassName.class 的 Scala 等价物是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!