Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个 arrayList 包含许多不同的对象,包括、 、Log等Frog。我想执行一些仅适用于实现和接口的类类型的操作,。TurtleRockIAction
Log
Frog
Turtle
Rock
IAction
有没有内置在 Java 中的东西可以做到这一点?我的尝试:
for(Object o : objectList){ if(o.getClass instanceof IAction){ // doesnt work // doWork } }
您检查实例,而不是类:
for (Object o : objectList){ if (o instanceof IAction) { // doWork } }