报错的提示是
"The abstract method nextLamp in type TrafficLamp can only set a visibility modifier, one of public or protected"
从这个提示来看,抽象的方法必须可见,修饰符必须是public或protected的一种
比如,换成这种形式完成这个功能就能加private
- public enum TrafficLamp{
- RED,GREEN,YELLOW;
-
- private TrafficLamp nextLamp(){
- switch(this){
- case RED: return GREEN;
- case GREEN: return YELLOW;
- case YELLOW: return RED;
- }
- return RED;
- }
- }
复制代码
但是,如果方法加了private就不能直接用了不怎么实用...仅供参考 |