泛型和方法

Mat*_*att 0 java generics

public void delete10(){
    T s = null;
    try {
        s=x.pop();          
        while(s.avg()<10)
            s=x.pop();
}
Run Code Online (Sandbox Code Playgroud)

我得到了The method avg() is undefined for the type T.现在我明白avg()并没有在类型T上实现,但它是在我将使用的类型而不是T这个泛型上实现的.我怎么能做对的?

public E pop() throws EmptyStackException {
    if (empty()) {
        throw new EmptyStackException();
    }
    E result = top.item;
    top = top.next;
    return result;
}
Run Code Online (Sandbox Code Playgroud)

Daw*_*ica 5

有一个avg列出的方法的接口.然后当你宣布T,写T extends yourInterface.