我有一组在类层次结构中管理数据库存储的类,如下所述,并且希望案例类能够访问伴随对象的父类中的受保护方法:
class TableBase[T] {
protected def insert(...):T {...}
protected def update(...) {...}
// Other "raw" CRUD-methods that I don't want the
// world to have access to
}
object User extends TableBase[User] {
}
case class User(id:Int, email:String) {
// But here it would be really useful to access the "raw" CRUD methods:
def changeEmail(newEmail:String) = User.update(...)
}
唯一的问题是 User.changeEmail 中对 User.update 的调用是非法的,因为 User(类)不在 TableBase 的继承链中:
method update in class TableBase cannot be accessed in object models.User
Access to protected method update not permitted because enclosing class
class User in package models is not a subclass of class TableBase in package
models where target is defined
有没有一种(方便的)方法来允许这种类型的调用?
现在我必须要么将 changeEmail 类型的函数移动到单例中,这使得调用代码相当冗长,要么复制方法签名。