Chinaunix首页 | 论坛 | 博客
  • 博客访问: 258177
  • 博文数量: 164
  • 博客积分: 60
  • 博客等级: 民兵
  • 技术积分: 1129
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-09 21:55
文章分类

全部博文(164)

文章存档

2017年(2)

2015年(67)

2014年(95)

我的朋友

分类: Java

2015-04-29 19:03:24


第一种:
自定义

点击(此处)折叠或打开

  1. public class Direction {
  2.     // 创建几个实例

  3.     public static final Direction FRONT = new Direction();
  4.     public static final Direction BEHIND = new Direction();
  5.     public static final Direction LEFT = new Direction();
  6.     public static final Direction RIGHT = new Direction();

  7.     // 构造私有,别人就不能无限的创建了

  8.     private Direction() {
  9.     }
  10. }
JDK

点击(此处)折叠或打开

  1. /*
  2.  * 通过JDK5提供的枚举来做枚举类
  3.  */
  4. public enum Direction {
  5.     FRONT, BEHIND, LEFT, RIGHT;
  6. }


第二种

点击(此处)折叠或打开

  1. public class Direction2 {
  2.     // 创建几个实例

  3.     public static final Direction2 FRONT = new Direction2("前");
  4.     public static final Direction2 BEHIND = new Direction2("后");
  5.     public static final Direction2 LEFT = new Direction2("左");
  6.     public static final Direction2 RIGHT = new Direction2("右");

  7.     // 构造私有,别人就不能无限的创建了

  8.     // private Direction2() {

  9.     // }


  10.     // 加入成员变量,并去掉无参构造

  11.     private String name;

  12.     private Direction2(String name) {
  13.         this.name = name;
  14.     }

  15.     public String getName() {
  16.         return name;
  17.     }
  18. }


点击(此处)折叠或打开

  1. /*
  2.  * 通过JDK5提供的枚举来做枚举类
  3.  */
  4. public enum Direction2 {
  5.     FRONT("前"), BEHIND("后"), LEFT("左"), RIGHT("右");

  6.     private String name;

  7.     private Direction2(String name) {
  8.         this.name = name;
  9.     }

  10.     public String getName() {
  11.         return name;
  12.     }


  13. }

第三种

点击(此处)折叠或打开

  1. public abstract class Direction3 {
  2.     // 创建几个实例

  3.     public static final Direction3 FRONT = new Direction3("前") {
  4.         @Override
  5.         public void show() {
  6.             System.out.println("前");
  7.         }

  8.     };
  9.     public static final Direction3 BEHIND = new Direction3("后") {
  10.         @Override
  11.         public void show() {
  12.             System.out.println("后");
  13.         }

  14.     };
  15.     public static final Direction3 LEFT = new Direction3("左") {
  16.         @Override
  17.         public void show() {
  18.             System.out.println("左");
  19.         }

  20.     };
  21.     public static final Direction3 RIGHT = new Direction3("右") {
  22.         @Override
  23.         public void show() {
  24.             System.out.println("右");
  25.         }

  26.     };

  27.     // 构造私有,别人就不能无限的创建了

  28.     // private Direction2() {

  29.     // }


  30.     // 加入成员变量,并去掉无参构造

  31.     private String name;

  32.     private Direction3(String name) {
  33.         this.name = name;
  34.     }

  35.     public String getName() {
  36.         return name;
  37.     }

  38.     // 加入抽象方法

  39.     public abstract void show();
  40. }


点击(此处)折叠或打开

  1. /*
  2.  * 通过JDK5提供的枚举来做枚举类
  3.  */
  4. public enum Direction3 {
  5.     FRONT("前") {
  6.         @Override
  7.         public void show() {
  8.             System.out.println("前");
  9.         }
  10.     },
  11.     BEHIND("后") {
  12.         @Override
  13.         public void show() {
  14.             System.out.println("后");
  15.         }
  16.     },
  17.     LEFT("左") {
  18.         @Override
  19.         public void show() {
  20.             System.out.println("左");
  21.         }
  22.     },
  23.     RIGHT("右") {
  24.         @Override
  25.         public void show() {
  26.             System.out.println("右");
  27.         }
  28.     };

  29.     private String name;

  30.     private Direction3(String name) {
  31.         this.name = name;
  32.     }

  33.     public String getName() {
  34.         return name;
  35.     }

  36.     public abstract void show();
  37. }


阅读(620) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~