简体   繁体   English

泛型和方法

[英]Generics and methods

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

I'm getting The method avg() is undefined for the type T .Now I understand avg() isn't implemented on type T but it's implemented on the types that I will use instead of T for this generic. 我正在获取The method avg() is undefined for the type T 。现在,我知道avg()不是在类型T上实现的,但它是在我将使用此类型而不是T的类型上实现的。 How can I make it right? 我该怎么做?

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

Have an interface with the avg method listed. 具有列出的avg方法的接口。 Then when you declare T , write T extends yourInterface . 然后,当您声明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