我有一个 var 的特征
trait Foo {
@Id var _id: String
}
现在我想初始化 var
class Bar(s: String) extends Foo {
_id = s
}
但我得到这个错误:
error: class Bar needs to be abstract, since variable _id in class Foo of type String
is not defined (Note that variables need to be initialized to be defined) class Bar(s: String) extends Foo {
关键是注释是在使用特征时继承的,我想使用它。我想要一些带有一些映射注释的特征,并让它们在子类中可用。
有人可以提供一个想法,提示,解决方案吗?
编辑:
我忘了写下extends Foo
示例中的注释,如评论中所述。所以这个例子是不完整的。
编辑:
当我var
在 Trait 中定义时,我可以简单地@Id var _id: String = _
写入并且注释被正确继承。但是现在不强制必须设置该字段。 Bar
_id = s