自定了一个Operation类,但是没办法用for(Operation operation :r.operations())循环,提示不兼容类型
import java.util.List;
public interface Robot {
String name();
String model();
List operations();
static void test(Robot r){
if (r instanceof Null)
System.out.println("[Null Robot]");
System.out.println("Robot name: " + r.name());
System.out.println("Robot model: " + r.model());
for (Operation operation : r.operations()){ //这里报错
System.out.println(operation.description.get());
operation.command.run();
}
}
}
不兼容的类型。实际为 Operation',需要 'java.lang.Object'
第6行改成
List<Operation> operations();
你就根据提示把大写的那个Opreation改成Object
增强for循环,对于你的代码,可以直接打r.operations().for就出来正确的了