我依赖于一个旧的 Java API,它有点糟糕,并且喜欢在数据丢失时抛出空指针异常。我想创建一个具有选项类型访问器但保留旧访问器的子类,直到我决定需要为它们创建安全访问器。有没有一种从原始对象的副本创建子类的好方法?我想实现以下目标:
SafeIssue extends Issue {
def safeMethod: Option[Value] = { //... }
}
val issue = oldapi.getIssue()
val safeIssue = SafeIssue(issue)
//Preserves issue's methods and data if I need them
val unsafeVal = safeIssue.unsafeMethod
val maybeVal = safeIssue.safeMethod