scala - 从字符串中获取类 [英] scala - get class from a string
本文介绍了scala - 从字符串中获取类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
从 Scala 开始,我使用的是一个需要类参数的 Java 库.示例:
From Scala, I'm using a Java library that expects a class argument. Example:
def service: OAuthService = new ServiceBuilder()
.provider(classOf[RunApi])
RunApi 是 Java 类.
RunApi is Java class.
不过,我希望能够将各种类传递给提供者.我有一个字符串格式的列表.
I'd like to be able pass a variety of classes to provider though. I have a list of them in String format.
例如,如果我知道 String 格式的 RunApi;例如com.me.RunApi",如何构造与上述代码等效的代码?
Example, if I know the RunApi in String format; e.g. "com.me.RunApi", how can construct the equivalent to above code?
推荐答案
使用 forName
方法:
scala> Class.forName("scala.collection.mutable.ArrayBuffer")
res0: java.lang.Class[_] = class scala.collection.mutable.ArrayBuffer
这篇关于scala - 从字符串中获取类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文