Jan*_*cki 2 constructor scala params
这是我目前不能令人满意的解决方案,在传递给超类构造函数之前操纵传递给子类构造函数的值的问题,
class MissingItemsException(items: Set[String], itemsCategory: String)
extends RuntimeException(MissingItemsException.makeMessage(items, itemsCategory))
private object MissingItemsException {
private def makeMessage(items: Set[String], itemsCategory: String): String = {
/* Format set as ['?','?','?'] */
val Items = items mkString ("['", "','", "']")
"the following items %s were missing from '%s'" format (items, itemsCategory)
}
}
Run Code Online (Sandbox Code Playgroud)
是否有一种方法可以分解转换,以便转换代码保持接近使用点,同时保持代码清晰?
您可以使用早期初始化程序:
class MissingItemsException(items: Set[String], itemsCategory: String) extends {
val customMessage = {
val Items = items mkString ("['", "','", "']")
"the following items %s were missing from '%s'" format (items, itemsCategory)
}
} with RuntimeException( customMessage );
Run Code Online (Sandbox Code Playgroud)
从老式的词汇范围角度来看,它甚至可以编译.但编译它确实,它做你想要的!然而,它是否比你的解决方案"更好"是一个品味问题.
| 归档时间: |
|
| 查看次数: |
86 次 |
| 最近记录: |