2

当我尝试以这种形式编译代码时,会生成编译错误“Incompatible types - found java.lang.Object but expected E”:

public class E {
    private int x;       

    public E {
        x = 0;
    }
}

public class Class {
    private E o;
    private E b;
    private Stack stack = new Stack();

    public void foo() {
        stack.push(o);
    }

    public void bar() {
        **b = stack.pop();**
    }
}
4

2 回答 2

5

你可以声明堆栈包含Es:

private Stack<E> stack = new Stack<E>();
于 2012-07-23T00:28:20.683 回答
2

尝试转换为 E。

public void bar() {
    b = (E)stack.pop();
}
于 2012-07-23T00:22:47.087 回答