아래에 설명 된대로 클래스 계층 구조에서 db 저장소를 관리하는 클래스 세트가 있고 사례 클래스가 컴패니언 객체의 부모 클래스에있는 보호 된 메소드에 액세스 할 수 있기를 바랍니다 : 스칼라 : 컴패니언 객체의 부모 보호 된 메소드 사용
유일한 문제는
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(...)
}
사용자 (클래스) TableBase에서 상속 체인에없는 때문에 User.changeEmail에 User.update에 대한 호출이 불법이라고한다
: A (편리한 있는가
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 유형의 함수를 싱글 톤으로 이동해야합니다.이 함수는 호출 코드를 다소 장황하게 만들거나 메소드 서명을 복제합니다.
패키지 수준에서 [private] 또는 [protected] 메소드 선언 (http://www.artima.com/pins1ed/packages-and-imports.html#lst:access-qualifiers) 옵션 제공 ? – DaoWen
이론 상으로는 그렇습니다. TableBase는 symlink-source-shared 프로젝트에 포함되어있어 다른 여러 프로젝트에서 사용하기 때문에 패키지 공간을 mangling하는 것이 엉망이됩니다. – Jxtps