【发布时间】:2015-09-15 04:53:03
【问题描述】:
我要做一个返回多个Object的ArrayList的方法,像这样:
public ArrayList<Object> getData(Object similar) {
//I suppose that I have 2 ArrayList that already contain data here
ArrayList<Human> humans = new ArrayList<Human>();
ArrayList<Animal> animals = new ArrayList<Animal>();
if (similar.getClass().equals(Human.class)) {
return humans;
}
if (similar.getClass().equals(Animal.class)) {
return animals;
}
return null;
}
我的想法是检查 Object 参数的类型,如果是 Human 类,则返回 Human 的 ArrayList。但是由于类型不兼容,我不能那样返回。
问题是返回对象不具体。
我如何解决它?谢谢!
【问题讨论】: