如何在Scala中引用未实例化的类?

时间:2012-03-07 20:09:49

标签: class scala types

您可以在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

假设我想直接设置the_class的值。我似乎能够声明一个正确类型的变量:

scala> var the_class: java.lang.Class[_ <: FooBar] = null
the_class: java.lang.Class[_ <: FooBar] = null

但我似乎无法将变量绑定到任何值。这可能吗?

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
                   ^

1 个答案:

答案 0 :(得分:5)

你的意思是:

val the_class = classOf[FooBar]