我想创建惰性序列以从 Scala 中的输入流中读取值。
import java.util._
object Main {
val input = new Scanner(System.in)
def main(args: Array[String]) {
val it = new MyIt().take(5)
}
class MyIt extends Iterator[Int] {
def hasNext = true
// def next = 29
def next = input.nextInt
}
}
当我将 next = 29 更改为 next = input.nextInt 时,它不会再编译并抱怨 MyIt 没有成员获取。看来我完全误解了一些东西。您能否给我提示(或者可能链接到关于惰性序列的好文章 - 谷歌有很多结果,但似乎垃圾太多 - 所以我迷路了)