gpt4 book ai didi

java - 我可以有一个返回泛型、泛型类型的方法吗?

转载 作者:行者123 更新时间:2023-11-30 07:56:50 26 4
gpt4 key购买 nike

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

我有以下类(class)

 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 泛型,并将该类定为最终类。我仍然想看看这种事情是否可能!

最佳答案

在您的 StoreLoader 类定义中,S 已被限制为存储类型(您创建实例所用的特定 T 的类型),因此当您在函数返回类型中写入“S”时,无需指定 T再次。

关于java - 我可以有一个返回泛型、泛型类型的方法吗? <T 延伸 ...<U>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32552636/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com