A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 我叫MT 中级黑马   /  2014-3-25 18:43  /  945 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 我叫MT 于 2014-3-25 21:40 编辑
  1. public class Demo{
  2.         public static void main(String[] args) {
  3.                 System.out.println(TrafficLight.GREEN);
  4.         }
  5.         public enum TrafficLight{
  6.                 RED{
  7.                         public TrafficLight nextLight() {
  8.                                 return GREEN;
  9.                         }
  10.                 },GREEN{
  11.                         public TrafficLight nextLight() {
  12.                                 return YELLOW;
  13.                         }
  14.                 },YELLOW{
  15.                         public TrafficLight nextLight() {
  16.                                 return RED;
  17.                         }
  18.                 };
  19.                 public abstract TrafficLight nextLight();
  20.         }
  21. }
复制代码
为什么调用GREEN时返回的不是yellow?哪里出问题了?

2 个回复

倒序浏览
请看注释
  1. public class Testt {

  2.         public static void main(String[] args) {
  3.                 System.out.println(TrafficLight.GREEN.nextLight());//注意这里!!!
  4.         }

  5.         public enum TrafficLight {
  6.                 RED {
  7.                         public TrafficLight nextLight() {
  8.                                 return GREEN;
  9.                         }
  10.                 },
  11.                 GREEN {
  12.                         public TrafficLight nextLight() {
  13.                                 return YELLOW;
  14.                         }
  15.                 },
  16.                 YELLOW {
  17.                         public TrafficLight nextLight() {
  18.                                 return RED;
  19.                         }
  20.                 };
  21.                 public abstract TrafficLight nextLight();
  22.         }

  23. }
复制代码
回复 使用道具 举报
需要调用对应的方法
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马