如何指定接口方法返回类型列表< whatever> [英] How do I specify interface method return type list<whatever>
问题描述
有趣的问题。我正在尝试创建一个数据库类,它将包含扩展数据库接口的各种数据库类的多个实例。每个数据库将包含一个列表或多个列表,每个列表包含不同的类型。
所以
Interesting problem. I am trying to creating a databanks class which will contain multiple instances of various databank classes which extend a databank interface. Each databank will contain a list or multiple lists each containing a different type.
So.
databank.getXdatabank.getData.add(y);
问题......
据我所知必须实现接口的所有方法。我希望不需要指定每个列表包含的类型,而是只返回一个列表。
The problem...
As I understand it all of the methods of an interface must be implemented. I was hoping not to need to specify the type that each list contains but rather that it just returns a list.
public interface Idatabank {
List<what do I put here> getData();
}
public Xdatabank implements Idatabank {
List<int> whatever1;
public List<int> getData() {
return this.whatever1;
}
}
public Zdatabank implements Idatabank {
List<String> whatever2;
public List<String> getData() {
return this.whatever2;
}
}
我的尝试:
我尝试在界面中输入
What I have tried:
I tried putting
List<?>
。
然后使用
in the interface.
Then with
container.getDataBanks().getXdatabank().getData().add(y);
该方法不适用于参数..
the method is not applicable for the arguments..
推荐答案
您需要参数化您的界面定义:
You need to parametrize your interface definition:
import java.util.List;
public interface Idatabank<e> {
List<e> getData();
}
然后定义如下实现:
Then define the implementation like this:
public class Xdatabank implements Idatabank<integer> {
List<integer> whatever1;
public List<integer> getData()
{
return whatever1;
}
}
List< object>如果没有更好的方法,我们会这样做。
List<object> will do if there is not a better way.
这篇关于如何指定接口方法返回类型列表< whatever>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!