Java 的 ClassName.class 的 Scala 等价物是什么? [英] What is the Scala equivalent of Java's ClassName.class?

查看:36
本文介绍了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?

推荐答案

有一个方法 classOfscala.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 等价物是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆