我在 line 处设置了一个断点,s = new Array(capacity)
但似乎没有调用 apply 方法。我是否正确实施了它?
object StacksAndQueuesTest {
def main(args: Array[String]) {
val f = new FixedCapacityStackOfStrings(3)
println(f.isEmpty);
}
}
class FixedCapacityStackOfStrings(capacity : Int) {
var s : Array[String] = _
var N : Int = 0
def isEmpty : Boolean = {
N == 0
}
def push(item : String) = {
this.N = N + 1
s(N) = item
}
def pop = {
this.N = N - 1
val item : String = s(N)
/**
* Setting this object to null so
* that JVM garbage collection can clean it up
*/
s(N) = null
item
}
object FixedCapacityStackOfStrings {
def apply(capacity : Int){
s = new Array(capacity)
}
}
}