繁体   English   中英

泛型和方法

[英]Generics and methods

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

我正在获取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;
}

具有列出的avg方法的接口。 然后,当您声明T ,写T extends yourInterface

假设avg方法是在接口Averageable中声明的,则您的类应声明为

public class MyClass<T extends Averageable>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM