现在有个Fruit父类,下面有各种水果apple,orange继承Fruit,给出一个具体水果的List,如何判读是否有某一个子类的实例
//判断fruiltList是否有?中给出的类,如果有返回该类(?是不知道形参该如何表示),
//比如?入参是Orange,要判断fruitList是否有Orange的实例,如果有则返回list中的Orange对象
public static <T extends Fruit> T getInstant(List<Fruit> fruitList,?){
}
public static void main(String[] args) {
List<Fruit> fruitList = new ArrayList<>();
Oranger orange = new Oranger("orange");
Apple apple= new Apple("apple");
fruitList.add(orange);
fruitList.add(apple);
//这边的入参怎么弄
Apple apple= getInstant(fruitList, Apple.class);
}
天蓬老师2017-04-18 09:39:43
Traversal, instanceof operator
Maybe so. . I haven’t learned java in depth
public static <T extends Fruit> T getInstant(List<Fruit> fruitList, Class c){
}
Reference: https://docs.oracle.com/javas...