Fork me on GitHub

枚举类

package com.raycloud.waimai.customer.center.enums;

import java.util.Objects;

/**
 * @className: TestEnum
 * @Descripetion: 简单枚举类展示
 * @Date:: 2019-06-03 15:00
 */

public enum TestEnum {

    NO_1(1,"jack",20),
    NO_2(2,"tom",18);

    private Integer id;
    private String name;
    private Integer age;

    TestEnum(Integer id, String name, Integer age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public static TestEnum getById(Integer id){
        for (TestEnum testEnum:TestEnum.values()){
            if (testEnum.getId().equals(id)){
                return testEnum;
            }
        }
        return null;
    }

    public static TestEnum getByName(String name){
        for (TestEnum testEnum:TestEnum.values()){
            if (testEnum.getName().equals(name)){
                return testEnum;
            }
        }
        return null;
    }

   /**
    * @Description //TODO 排除空指针异常
    * @Param [id]
    **/
    public static TestEnum getObject(Integer id){
        for (TestEnum testEnum:TestEnum.values()){
            if (Objects.equals(testEnum.getId(),id)){
                return testEnum;
            }
        }
        return null;
    }


    public static void main(String[] args) {
        System.out.println(TestEnum.getById(1).getName());
        System.out.println(TestEnum.getByName("jack").getId());
        System.out.println(TestEnum.getObject(1).getName());
    }

}

  

posted @ 2019-06-03 15:18  雪人奥特曼  阅读(90)  评论(0编辑  收藏  举报