枚举举例

/**
 * 枚举类型的特点
 * 1、枚举类型有一个公共的基本的父类,是 java.lang.Enum 类型,所以不能再继承别的类型
 * 2、枚举类型的构造器必须是私有的
 * 3、枚举类型可以实现接口
 * 
 * @author kakaluote
 * @date 2021年6月30日 上午9:07:41
 */
public enum SunFamily {

	SUWUKONG(1,"孙悟空",50,"sunwukong@beijita.sy","1201-02-26"),
	SUWUFAN(2,"孙悟饭",20,"sunwufan@beijita.sy","1231-05-06"),
	SUWUTIAN(3,"孙悟天",12,"sunwutian@beijita.sy","1239-10-21"),
	QIQI(4,"琪琪",47,"qiqi@beijita.com","1204-03-26");
	
	private Integer id;
	private String username;
	private Integer age;
	private String email;
	private String birthday;
	
	public static SunFamily getPeople(int index){
		SunFamily[] sunFamilies = SunFamily.values();
		for (SunFamily sunFamily : sunFamilies) {
			if(index == sunFamily.getId()){
				return sunFamily;
			}
		}
		return null;
	}
	
	public static void main(String[] args) {
		System.out.println(SunFamily.getPeople(1).getUsername());
	}
	//构造器必须私有
	private SunFamily(Integer id, String username, Integer age, String email,
			String birthday) {
		this.id = id;
		this.username = username;
		this.age = age;
		this.email = email;
		this.birthday = birthday;
	}
	
	public Integer getId() {
		return id;
	}

	public String getUsername() {
		return username;
	}

	public Integer getAge() {
		return age;
	}

	public String getEmail() {
		return email;
	}

	public String getBirthday() {
		return birthday;
	}
}

枚举举例

上一篇:数据结构 ------- 简介


下一篇:UGUI六大基础组件——Graphic Raycaster