所以我有一个没有构造函数的类,我想知道默认构造如何初始化值。将elementWithCachedMax初始化为新的LinkedList吗?怎么样,为什么?我在哪里可以了解有关默认构造函数如何初始化成员变量的更多信息?
public static class Stack {
// Stores (element, cached maximum) pair.
private Deque<ElementWithCachedMax> elementWithCachedMax
= new LinkedList<>();
public boolean empty() { return elementWithCachedMax.isEmpty(); }
public Integer max() {
if (empty()) {
throw new IllegalStateException("max(): empty stack");
}
return elementWithCachedMax.peek().max;
}
public Integer pop() {
if (empty()) {
throw new IllegalStateException("pop(): empty stack");
}
return elementWithCachedMax.removeFirst().element;
}
public static void print(){
System.out.println("HI");
}
public void push(Integer x) {
elementWithCachedMax.addFirst(
new ElementWithCachedMax(x, Math.max(x, empty() ? x : max())));
}
}