2013-11-25 146 views
0
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; 
} 
+2

你可以粘貼[SSCCE(http://sscce.org/) –

+1

能否請你發佈你的類應該聲明類定義(即公共類Foo ...) – MichaelMilom

回答

3

假設平均法在接口Averageable聲明,爲

public class MyClass<T extends Averageable> 
5

與列出的avg方法有接口。然後當你聲明T時,寫T extends yourInterface