先看一个枚举类的代码:- package game;
- public enum Chessmen {
- WHITE("○"),BLACK("●");
- private String chessmen;
- private Chessmen(String chessmen)
- {
- this.chessmen = chessmen;
- }
- public String getChessmen()
- {
- return chessmen;
- }
- }
复制代码 说明:枚举类用enum代替了class,构造方法用的是private,目的就是为了创建对象的唯一性。
转化为class类如下:- package game;
- public class Chessmen1 {
- public final static Chessmen1 WHITE = new Chessmen1("○");
- public final static Chessmen1 BLACK = new Chessmen1("●");
- private String chessmen1;
- private Chessmen1(String chessmen1) {
- this.chessmen1 = chessmen1;
- }
- public String getChessmen1() {
- return chessmen1;
- }
- }
复制代码 测设代码如下:- package game;
- public class Test {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- String white = Chessmen.WHITE.getChessmen();
- String black = Chessmen.BLACK.getChessmen();
- System.out.println(white + black);
- System.out.println("/**************************/");
- String white1 = Chessmen.WHITE.getChessmen();
- String black1 = Chessmen.BLACK.getChessmen();
- System.out.println(white1 + black1);
- }
- }
复制代码 |
|