scala - 在 Scala 中根据类名动态实例化一个类
问题描述
我有一组类,每个类都有一个名为Description
.
我希望我的图书馆的最终用户能够指定他们感兴趣的类并能够阅读该描述。因此,我希望能够动态实例化每个指定的类并返回Description
.
我知道我需要反思才能做到这一点,并且我已经看到很多 SO 问题建议Class.forName
可以帮助我,但我无法让它发挥作用。
希望以下内容展示了我想要做的事情:
scala> abstract class Feature (start: Short, end: Short){val description: String}
defined class Feature
scala> class Feature1 (start: Short, end: Short) extends Feature(start, end){override val description = "this is feature 3"}
defined class Feature1
scala> class Feature2 (start: Short, end: Short) extends Feature(start, end){override val description = "this is feature 2"}
defined class Feature2
scala> class Feature3 (start: Short, end: Short) extends Feature(start, end){override val description = "this is feature 3"}
defined class Feature3
scala> val classesForWhichIWantToGetTheDescription = Set[String]("Feature1", "Feature2")
classesForWhichIWantToGetTheDescription: scala.collection.immutable.Set[String] = Set(Feature1, Feature2)
scala> val classesWithDescriptions = classesForWhichIWantToGetTheDescription.map(
| className => (className, s"I want the class description of ${className} to be displayed here")
| )
classesWithDescriptions: scala.collection.immutable.Set[(String, String)] = Set((Feature1,I want the class description of Feature1 to be displayed here), (Feature2,I want the class description of Feature2 to be displayed here))
scala> classesWithDescriptions.foreach(
| c => println(c)
| )
(Feature1,I want the class description of Feature1 to be displayed here)
(Feature2,I want the class description of Feature2 to be displayed here)
谁能帮我实现这一目标?
提前致谢
解决方案
尝试
val classesWithDescriptions = classesForWhichIWantToGetTheDescription.map(
className => {
val clazz = Class.forName(className)
val field = clazz.getDeclaredField("description")
field.setAccessible(true)
val constructor = clazz.getConstructor(classOf[Short], classOf[Short])
val instance = constructor.newInstance(0: Short, 0: Short)
val description = field.get(instance)
(className, s"description of $className: $description")
}
)
推荐阅读
- python-requests - 是否可以重试不同状态码的 GET 方法和不同状态码的 PUT/POST 方法?
- python - 带有自定义功能问题的 Django 动态模板
- python - 如何在 python 中获取 API 身份验证?
- reactjs - 单击按钮时如何呈现特定的图表类型?
- android-layout - 当两个 TextView 使用不同的 textSize 使用 RelativeLayout 时,layout_alignBottom 不起作用
- deep-learning - 有什么方法可以从 .qdrep 中提取内核操作名称(在 NVIDIA Nsight 系统中使用)?
- node.js - 更正 VS Code 的 launch.json 以附加到正在运行的 nodejs 进程的端口
- python - 根据 Pandas Datetime 中的财政年度过滤行
- android - 单击按钮启动后置摄像头?
- amazon-web-services - AWS API Gateway 的 create_api_key 需要什么权限?