您可以在Scala中执行以下操作非常简洁:
scala> class FooBar
defined class FooBar
scala> val a = new FooBar
a: FooBar = FooBar@7efeedca
scala> val the_class = a.getClass
the_class: java.lang.Class[_ <: FooBar] = class FooBar
scala> val b = the_class.newInstance
b: FooBar = FooBar@1ef1df56
Run Code Online (Sandbox Code Playgroud)
假设我想the_class直接设置值.我似乎能够声明一个正确类型的变量:
scala> var the_class: java.lang.Class[_ <: FooBar] = null
the_class: java.lang.Class[_ <: FooBar] = null
Run Code Online (Sandbox Code Playgroud)
但我似乎无法将变量绑定到任何值.这可能吗?
scala> the_class = class FooBar
<console>:1: error: illegal start of simple expression
the_class = class FooBar
^
scala> the_class = FooBar
<console>:9: error: not found: value FooBar
the_class = FooBar
^
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
141 次 |
| 最近记录: |