如何将 instanceof 与多个对象参数一起使用?

How to use instanceof with multiple object parameters?

错误消息不兼容的条件操作数类型 BM[] 和 SM

public void count(BM... bm) {

        int countSM = 0;
        int countKM = 0;

        System.out.println(bm.length);

        if (bm instanceof SM) {
            System.out.println("Von SM");
            countSM++;
            System.out.println(countSM);
        } else if (bm instanceof KM) {
            System.out.println("Von KM");
            countKM++;
            System.out.println(countKM);
        }

    }

我要统计打印出来,这个具体class的对象在参数

中有多少个

试试这个:

public void count(BM... bm) {

    int countSM = 0;
    int countKM = 0;

    System.out.println(bm.length);
    for(BM bm_object : bm)
        if (bm_object instanceof SM) {
            System.out.println("Von SM");
            countSM++;
            System.out.println(countSM);
        } else if (bm_object instanceof KM) {
            System.out.println("Von KM");
            countKM++;
            System.out.println(countKM);
        }

    }
}