技術(shù)匯總:第十八章:枚舉的簡單使用

結(jié)合上一章閱讀:https://blog.csdn.net/java_wxid/article/details/99168098

枚舉代碼:

    package com.javaliao.backstage;
     
    import lombok.Getter;
     
    public enum MyData {
     
        ONE(1,"齊國"),TWO(2,"楚國"),THREE(3,"燕國"),FOUR(4,"趙國"),FIVE(5,"魏國"),SIX(6,"韓國");
        //提供get方法
        @Getter private Integer retCode;
        @Getter private String retMessage;
        //給上構(gòu)造器
        MyData(Integer retCode, String retMessage) {
            this.retCode = retCode;
            this.retMessage = retMessage;
        }
     
        public static MyData forEatch_MyData(Integer retCode){
            MyData[] values = MyData.values();
            for (MyData value : values) {
                if(retCode == value.getRetCode()){
                    return value;
                }
            }
            return null;
        }
     
    }

使用枚舉的代碼:

    import java.util.concurrent.CountDownLatch;
     
     
    public class Demo {
     
        public static void main(String[] args) throws Exception{
            CountDownLatch countDownLatch = new CountDownLatch(5);
            for (int i = 1; i <= 6; i++) {
                new Thread(()->{
                    System.out.println(Thread.currentThread().getName()+"\t 被滅");
                    countDownLatch.countDown();
                },MyData.forEatch_MyData(i).getRetMessage()).start();
            }
            countDownLatch.await();
            System.out.println(Thread.currentThread().getName()+"\t 秦國一統(tǒng)華夏");
        }
    }

控制臺: