jdk1.5新特性5之枚举之模拟枚举类型

 

模拟方式一

 

package cn.xy.Enum;

public class TrafficLampEasy
{
 private int time;

 public final static TrafficLampEasy REDLAMP = new TrafficLampEasy(20);
 public final static TrafficLampEasy GREENLAMP = new TrafficLampEasy(20);
 public final static TrafficLampEasy YELLOWLAMP = new TrafficLampEasy(5);

 public TrafficLampEasy()
 {
 }

 public TrafficLampEasy(int time)
 {
  this.time = time;
 }

 public TrafficLampEasy nextLamp()
 {
  TrafficLampEasy result = new TrafficLampEasy();

  if (this == REDLAMP)
  {
   result = GREENLAMP;
  }
  else if (this == GREENLAMP)
  {
   result = YELLOWLAMP;
  }
  else if (this == YELLOWLAMP)
  {
   result = REDLAMP;
  }
  return result;
 }

 public String getValue()
 {
  String result = "";

  if (this == REDLAMP)
  {
   result = "红灯,时长:" + time;
  }
  else if (this == GREENLAMP)
  {
   result = "绿灯,时长:" + time;
  }
  else if (this == YELLOWLAMP)
  {
   result = "黄灯,时长:" + time;
  }
  return result;
 }

 public int getTime()
 {
  return time;
 }

 public void setTime(int time)
 {
  this.time = time;
 }
}

 

TrafficLampEasy teRed = TrafficLampEasy.REDLAMP;
System.out.println(teRed.getValue());
System.out.println(teRed.nextLamp().getValue());

从这个例子中我们看出,枚举类型其实就是一个类返回该类本身

 

 

模拟方式二

package cn.xy.Enum;

public abstract class TrafficLamp
{
 /**
  * 下一个灯
  */
 public abstract TrafficLamp nextLamp();

 /**
  * 获取值
  */
 public abstract String getValue();

 /**
  * 时长
  */
 private int time;

 public TrafficLamp()
 {
 }

 public TrafficLamp(int time)
 {
  this.time = time;
 }

 /**
  * 红灯,匿名类,相当于继承TrafficLamp抽象类,并实现抽象方法
  */
 public final static TrafficLamp REDLAMP = new TrafficLamp(50) {
  @Override
  public TrafficLamp nextLamp()
  {
   return GREENLAMP;
  }

  @Override
  public String getValue()
  {
   return "红灯,时长:" + this.getTime();
  }
 };

 public final static TrafficLamp GREENLAMP = new TrafficLamp(50) {
  @Override
  public TrafficLamp nextLamp()
  {
   return YELLOWLAMP;
  }

  @Override
  public String getValue()
  {
   return "绿灯,时长:" + this.getTime();
  }
 };

 public final static TrafficLamp YELLOWLAMP = new TrafficLamp(2) {
  @Override
  public TrafficLamp nextLamp()
  {
   return REDLAMP;
  }

  @Override
  public String getValue()
  {
   return "黄灯,时长:" + this.getTime();
  }
 };

 public int getTime()
 {
  return time;
 }

 public void setTime(int time)
 {
  this.time = time;
 }
}

TrafficLamp red = TrafficLamp.REDLAMP;
System.out.println(red.getValue());
System.out.println(red.nextLamp().getValue());

 

 

采用匿名类的方式,那么什么是匿名类呢?

 

匿名类适合那些只需要使用一次的类

public abstract class AnonymousClassDesk
{
 public abstract double getPrice();

 public abstract String getName();

}

 

public class Desk extends AnonymousClassDesk
{
 @Override
 public double getPrice()
 {
  return 100;
 }

 @Override
 public String getName()
 {
  return "普通书桌";
 }

}

 public static void main(String[] args)
 {
   AnonymousClassDesk desk = new AnonymousClassDesk() {

   @Override
   public double getPrice()
   {
    return 100;
   }

   @Override
   public String getName()
   {
    return "匿名书桌";
   }
  };

  System.out.println(desk.getName());
 }

 

不仅可以使抽象类,也可以是接口。匿名类没有什么特别的地方,同样还是要实现需要实现的方法。

 

上一篇:FTPS(基于 SSL 的FTP)与 SFTP(SSH 文件传输协议)对比


下一篇:开源IaaS ZStack本地存储使用手册