【发布时间】:2012-09-06 00:42:39
【问题描述】:
我有一组在类层次结构中管理数据库存储的类,如下所述,并且希望案例类能够访问伴随对象的父类中的受保护方法:
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 类型的函数移动到单例中,这使得调用代码相当冗长,要么复制方法签名。
【问题讨论】:
-
声明方法
privateorprotectedat the package-level是一个选项吗? -
理论上是的。这需要相当多的重新调整 - TableBase 位于一个单独的符号链接源共享项目中,被其他几个项目使用,因此破坏包空间将是一团糟。
标签: scala