我可以使用返回通用类型泛型的方法吗? <t exends =“”... <u =“”>

时间:2015-09-13 17:40:52

标签: java generics inheritance

我的问题正是我的标题所说的。

我有以下课程

 public class StoreItem
 {
     //Lots of irrelevant information! :)
 }

 public class Store<T extends StoreItem>
 {
    private ArrayList<T> storeItems;
    //Other irrelevant information.
 }

 public abstract class StoreLoader<T extends StoreItem, S extends Store<T>> 
 {
     //Parsed xml document info obtained from file in constructor
     public StoreLoader (File toLoadFrom)
     {

     }

     //Here is where my question begins. I would like to do the following which states the method will return the same generic S that extends Story<of generic type T that extends storyitem>. I want to use the same T and S generics from the class definition above.
     public S<T> load ()
     {
         Store<T> store = new Store<T>(.....);
         //COde to read and process Store XML file
         return store;
     }
 }
编辑:我已经删除了S generic,并使该类成为最终版。我仍然想看看是否有可能出现这种情况!

1 个答案:

答案 0 :(得分:1)

在您的StoreLoader类定义中,S已经被限制为类型存储(您创建实例的特定T的类型),因此当您编写&#39; S&#39;在函数返回类型中,您不需要再次指定T.