我正在尝试解决“不耐烦的 Scala”一书给出的问题,该书要求将 java 的 BufferedInputStream 实现为特征。这是我的实现,
trait Buffering {
this:InputStream =>
private[this] val bis = {
new JavaBufferedInputStream(this)
}
override def read = bis.read
override def read(byte:Array[Byte], off:Int, len:Int) = bis.read(byte, off, len)
override def available = bis.available
override def close() {
bis.close
}
override def skip(n:Long) = bis.skip(n)
}
def main(args:Array[String]) {
val bfis = new FileInputStream(new File("foo.txt")) with Buffering
println(bfis.read)
bfis.close
}
但这给了我一个 java stackoverflow 错误,那么它有什么问题呢?谢谢!