【发布时间】:2018-12-26 20:47:25
【问题描述】:
为什么queue.get() 返回空列表?
class MyQueue{
var queue=List[Int](3,5,7)
def get(){
this.queue.head
}
}
object QueueOperator {
def main(args: Array[String]) {
val queue=new MyQueue
println(queue.get())
}
}
如何获得第一个元素?
【问题讨论】:
-
在你学习的时候,你可能会发现显式写出比必要的更多的类型很有用。在这种情况下,如果您编写了
def get(): Int { this.queue.head },编译器会告诉您get方法存在问题。