我有一个具有自定义特征的类型定义:
trait Foo {
def bar: String
}
val BooleanType: Class[Boolean] with Foo = classOf[Boolean] with Foo {
def bar = ""
}
//Error: error: ';' expected but 'with' found.
但是,这不起作用。我也尝试过:
val BooleanType: Class[Boolean] with Foo = classOf[Boolean] {...}
//Error: Class[Boolean](classOf[scala.Boolean]) does not take parameters
和
val BooleanType: Class[Boolean] with Foo = classOf[Boolean] with new Foo {...}
//Error: error: ';' expected but 'with' found.
没有办法。如何做到这一点?
编辑:我也尝试使用scala类型:
val Boolean: Type with Foo = typeOf[Boolean] with Foo {...}
//error: ';' expected but 'with' found.
您不能创建的子类,Class
因为它是最终类。如果可以,并且具有构造函数,则语法为:
val BooleanType: Class[Boolean] with Foo = new Class[Boolean](< args >) with Foo {
def bar = ""
}
据我所知,不可能对特定实例进行子类化(就像您尝试使用classOf[Boolean]
)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句