开发者

Parent fields initialization

In the following example a refere开发者_Python百科nce to reason is stored in both parent and child. I'd like to avoid this and store the reference only in parent (usual use of Java exceptions).

import java.lang.{Exception, Throwable}
class FileError(message: String, reason:Throwable) extends Exception(message, reason) {
  ...
}

How do I initialize a parent class field without storing a value in the child class object?


If you never use reason in the FieldError class, then it will not be stored in FieldError.

import java.lang.{Exception, Throwable}
class FileError(message: String, reason:Throwable) extends Exception(message, reason)

--

brianhsu@NBGentoo ~ $ scalac -print test.scala 
[[syntax trees at end of cleanup]]// Scala source: test.scala
package <empty> {
  class FileError extends java.lang.Exception with ScalaObject {
    def this(message: java.lang.String, reason: java.lang.Throwable): FileError = {
      FileError.super.this(message, reason);
      ()
    }
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜