2013-03-05 94 views
-2

我是新來的泛型。我想編寫一個接口SearchableFruit像許多「果」類:通用java界面

public interface SearchableFruit<T>{ 
    //returns a list of newer fruit object than current fruit object 
    public static List<T> searchNewerFruit(T curr); 
} 

所以,我可以用這個接口的類AppleOrange

public class Apple implements SearchableFruit{ 
    public static List<Apple> searchNewerFruit(Apple currentApple){ 
    //TODO get apples newers than currentApple 
    //return a list of Apples 
} 

} 

我以前從未和該做的界面是不是在爲我工作,你能澄清我該怎麼寫嗎?

有什麼建議嗎? 謝謝!

+5

問題是什麼? – benzonico 2013-03-05 15:21:49

+0

可能的重複:http://stackoverflow.com/questions/3933156/java-generics-and-interfaces – 2013-03-05 15:23:09

+1

注意:接口不能有靜態方法。 – Pyranja 2013-03-05 15:23:32

回答

0

它應該是這樣的

public interface SearchableFruit<T>{ 
    public List<T> searchNewerFruit(T curr); 
} 

public class Apple implements SearchableFruit<Apple> { 
    public List<Apple> searchNewerFruit(Apple currentApple){ 
     // impl 
    } 
} 
0

首先,接口不能包含靜態方法。 其次,你錯過了實施類型。更正:

public class Apple implements SearchableFruit<Apple>{ 
    public List<Apple> searchNewerFruit(Apple currentApple){ 
    //TODO get apples newers than currentApple 
    //return a list of Apples 
} 

} 
+0

ctrl + c ctrl + v pattern ... ech ... – 2013-03-05 18:38:56