閱讀830 返回首頁    go 京東網上商城


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());
 }

 

不僅可以使抽象類,也可以是接口。匿名類沒有什麼特別的地方,同樣還是要實現需要實現的方法。

 

最後更新:2017-04-04 07:32:09

  上一篇:go Java類加載器學習2——自定義類加載器和父類委托機製帶來的問題
  下一篇:go java線程學習1——線程基本概念和操作