enum constant specific method

import java.util.EnumSet;

public class ConstantSpecificMethod {
      private static enum Cycle {
    	  UNDERBODY {
			@Override
			void action() {
				System.out.println("Underbody");
			}
    	  },
    	  WHEELWASH {
			@Override
			void action() {
				System.out.println("Wheelwash");
			}
    	  },
    	  OVERRIDE {
			@Override
			void action() {
				System.out.println("Override");
			}
			/**重写父类方法**/
			public String toString() {
				return "override";
			}
    	  };
    	  abstract void action();
      }
      EnumSet<Cycle> cycles = EnumSet.of(Cycle.WHEELWASH);
      public void add(Cycle cycle) {
    	  cycles.add(cycle);
      }
      public void print() {
    	  for(Cycle c : cycles) {
    		  c.action();
    	  }
      }
      @Override
    public String toString() {
    	return cycles.toString();
    }
    public static void main(String[] args) {
    	ConstantSpecificMethod csm = new ConstantSpecificMethod();
    	System.out.println(csm);
    	csm.print();
    	csm.add(Cycle.UNDERBODY);
    	csm.add(Cycle.UNDERBODY);
    	csm.add(Cycle.OVERRIDE);
    	/**EnumSet中的排序时按出现在枚举类中的顺序**/
    	System.out.println(csm);//输出结果:[UNDERBODY, WHEELWASH, override]。。枚举类默认toString返回的是实例名
    	csm.print();
	}
}



你可能感兴趣的:(enum constant specific method)