我希望子类在基类'apply方法中注入功能:
基类:
case class BaseClass(data: Json)
object BaseClass {
def apply(data: String) : JsonClass = {
try {
_ //subclass functionality should go here
} catch {
case e: Exception => ErrorJsonClass(data)
}
}
}
object SubClass extends BaseClass {
def apply(data: String) : Json = {
deserialize(data) // this should be called within the base class' apply
}
}
有没有办法在不在基类中声明单独的方法的情况下执行此操作?
IE中。我试图避免以下情况:
object BaseClass {
def apply(data: String) : JsonClass = {
try {
convert(data)
} catch {
case e: Exception => ErrorJsonClass(data)
}
}
def convert(data: String): JsonClass = _
}
object SubClass extends BaseClass {
def convert(data: String) : Json = {
deserialize(data)
}
}