1

Java Exception クラスを拡張しようとしていますが、結果はかなり醜くて扱いにくいものです。ですから、私が正しい軌道に乗っているかどうかはわかりません。これを行うより良い方法はありますか?

class ParentError(_d: Double, msg: String, cause: Throwable)
      extends Exception(msg: String, cause: Throwable) {
  def this() = this(0, null, null)
  def this(d: Double) = this(d, null, null)
  def this(msg: String) = this(0, msg, null)
  def this(msg: String, cause: Throwable) = this(0, msg, cause)
  def this(cause: Throwable) = this(0, null, cause)
  def i = _d
}

case class ChildError(_b: Boolean, _i: Double, msg: String, cause: Throwable)
      extends ParentError(_i: Double, msg: String, cause: Throwable) {
  def this(b: Boolean) = this(b, 0, null, null)
  def this(msg: String) = this(false, 0, msg, null)
  def this(msg: String, cause: Throwable) = this(false, 0, msg, cause)
  def this(cause: Throwable) = this(false, 0, null, cause)
  def b = _b
}

注: In Scala を見たのですが、複数のコンストラクターを持つ Java クラスをサブクラス化するにはどうすればよいですか? しかし、それは私が探しているものではないと思います。ありがとう

4

1 に答える 1

5

たとえば、次のようにデフォルト値を使用します。

class ParentError(i: Double = 0, msg: String = null, cause: Throwable = null)
      extends Exception(msg: String, cause: Throwable) {
      ⋮
}
于 2013-06-21T19:12:43.930 に答える