search

Home  >  Q&A  >  body text

JAVA判断某个类是指定类的实例

现在有个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);
        
}

PHP中文网PHP中文网2781 days ago773

reply all(2)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-04-18 09:39:43

    instanceOf

    reply
    0
  • 天蓬老师

    天蓬老师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...

    reply
    0
  • Cancelreply