java:通过code获取枚举

java:通过code获取枚举_第1张图片

package com.nuole.holiday.test;

import com.nuole.holiday.enmu.CurrencyEnum;

import java.net.URL;
import java.net.URLClassLoader;

/**
 * @author ymgu
 * @Package com.nuole.holiday.test
 * @date 2023/7/27 11:18
 */
public class ClassPathTest {
    public static void main(String[] args) {
        CurrencyEnum currencyEnum=CurrencyEnum.getInstance("36");
        System.out.println(currencyEnum.getName());
        System.out.println(currencyEnum.getProcess());
    }
}

package com.nuole.holiday.enmu;

import java.util.Arrays;

/**
 * @author ymgu
 * @Package com.nuole.holiday.enmu
 * @date 2023/7/28 11:50
 */
public enum CurrencyEnum {
    AUD("36","澳大利亚元","AUD"),
    ATS("40","奥地利先令","ATS"),
    BOB("68","玻利维亚诺","BOB");
    private String code;
    private String name;
    private String process;
    CurrencyEnum(String code,String name,String process){
        this.code = code;
        this.name = name;
        this.process = process;
    }

    public static CurrencyEnum getInstance(String code){
        return Arrays.stream(CurrencyEnum.values())
                .filter(currencyEnum -> currencyEnum.getCode().equals(code))
                .findFirst()
                .orElse(null);
    }

    public String getCode() {
        return code;
    }

    public String getName() {
        return name;
    }

    public String getProcess() {
        return process;
    }


}

你可能感兴趣的:(java,python,开发语言)