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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 跃动 中级黑马   /  2015-5-11 12:02  /  2329 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

5黑马币
   在Java Project中,用的JRE System Library版本不能建立枚举类(已解决此问题),现在想用普通方法建立交通灯的Lamp类的代码,请问要怎么实现呢?求解ing...

最佳答案

查看完整内容

还没试,应该差不离了

3 个回复

倒序浏览
  1. package com.itheima.demo1;

  2. public class Lamp {
  3.         /*
  4.          * S2N,S2W,E2W,E2S是本类的核心,控制好他们就控制好了信号灯
  5.          */
  6.         public final Lamp S2N = new Lamp("N2S", "S2W", false);
  7.         public final Lamp S2W = new Lamp("N2E", "E2W", false);
  8.         public final Lamp E2W = new Lamp("W2E", "E2S", false);
  9.         public final Lamp E2S = new Lamp("W2N", "S2N", false);

  10.         // S2N,S2W,E2W,E2S对应的oppo对象
  11.         public final Lamp N2S = new Lamp(false);
  12.         public final Lamp N2E = new Lamp(false);
  13.         public final Lamp W2E = new Lamp(false);
  14.         public final Lamp W2N = new Lamp(false);

  15.         // 右转信号灯
  16.         public final Lamp S2E = new Lamp(true);
  17.         public final Lamp E2N = new Lamp(true);
  18.         public final Lamp N2W = new Lamp(true);
  19.         public final Lamp W2S = new Lamp(true);

  20.         private String next;
  21.         private String oppo;
  22.         private boolean isGreen;

  23.         // 私有化构造器
  24.         private Lamp(String oppo, String next, boolean isGreen) {
  25.                 this.oppo = oppo;
  26.                 this.next = next;
  27.                 this.isGreen = isGreen;
  28.         }

  29.         // 私有化构造器
  30.         private Lamp(boolean isGreen) {
  31.                 this.isGreen = isGreen;
  32.         }

  33.         /*
  34.          * 通过信号灯名获得得对应信号灯
  35.          */
  36.         public Lamp getLamp(String lamp) {
  37.                 switch (lamp) {
  38.                 case "S2N":
  39.                         return S2N;
  40.                 case "S2W":
  41.                         return S2W;
  42.                 case "E2W":
  43.                         return E2W;
  44.                 case "E2S":
  45.                         return E2S;
  46.                 default:
  47.                         return null;// 不会被执行
  48.                 }
  49.         }

  50.         public boolean isGreen() {
  51.                 return isGreen;
  52.         }

  53.         public void toGreen() {
  54.                 this.isGreen = true;
  55.                 getLamp(oppo).isGreen = true;
  56.                 System.out.println(this.toString() + "is green.");
  57.         }

  58.         public Lamp toRed() {
  59.                 this.isGreen = false;
  60.                 getLamp(oppo).isGreen = false;

  61.                 getLamp(next).isGreen = true;
  62.                 getLamp(getLamp(next).oppo).isGreen = true;
  63.                 return getLamp(next);
  64.         }

  65.         @Override
  66.         public String toString() {
  67.                 if (this == S2N) {
  68.                         return "S2N";
  69.                 } else if (this == S2W) {
  70.                         return "S2W";
  71.                 } else if (this == E2W) {
  72.                         return "E2W";
  73.                 } else if (this == E2S) {
  74.                         return "E2S";
  75.                 }
  76.                 return null; // 不会被执行
  77.         }
  78. }
复制代码

还没试,应该差不离了
回复 使用道具 举报
看了高新技术的枚举教程,彻悟
回复 使用道具 举报
赞赞赞!!!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马