不是真的。如果你把它们放进
val
s(名称必须以大写开头)您可以
val FooClass = classOf[Foo]
val BarClass = classOf[Bar]
// Return an object of the given class type:
def badFactory( clazz: Option[Class[_]]): AnyRef = {
clazz match {
case Some(FooClass) => new Foo()
case Some(BarClass) => new Bar()
case _ => ...
}
}
或避免重复
Some
:
// Return an object of the given class type:
def badFactory( clazz: Option[Class[_]]): AnyRef = {
clazz match {
case Some(clazz2) =>
if (clazz2 == classOf[Foo])
new Foo()
else if (clazz2 == classOf[Bar])
new Bar()
else ...
case None => ...
}
}