在 Scala 中,特征是否可以引用它所混入的类的命名构造函数参数?下面的代码无法编译,因为 ModuleDao 的构造函数参数不是特征中定义的 val。如果我val
在构造函数参数之前添加以使其公开,它会与特征中的那个匹配并编译,但我不希望将其设置为val
.
trait Daoisms {
val sessionFactory:SessionFactory
protected def session = sessionFactory.getCurrentSession
}
class ModuleDao(sessionFactory:SessionFactory) extends Daoisms {
def save(module:Module) = session.saveOrUpdate(module)
}
/* Compiler error:
class ModuleDao needs to be abstract, since value sessionFactory in trait Daoisms of type org.hibernate.SessionFactory is not defined */
// This works though
// class ModuleDao(val sessionFactory:SessionFactory) extends Daoisms { ... }