public AlertStatus nextStatus(){
int randNum = randNumGen.nextInt(3);
switch(randNum){
case 0: return new AlertStatusGreen();
case 1: return new AlertStatusYellow();
case 2: return new AlertStatusRed();
default: System.out.println("ERROR: no random number.");
}
}
这是我为学校制作程序中一个类中的方法。switch语句使用随机整数来返回一个派生自AlertStatus类的某个类的对象。
但是某些原因导致代码块中最后一行(上面代码中的第9行)出现“missing return statement }” 的错误提示。我不理解为什么会提示这个错误,因为我已经为每种情况都添加了return语句。
randNumGen
的类型是一个interface
,并且nextInt(int)
是一个抽象方法,那么编译器就不知道nextInt
的实际实现。例如,它可能是在运行时解析实现的。 - MC Emperor