• 枚举举例


    image

    /**
     * 枚举类型的特点
     * 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;
    	}
    }
    
  • 相关阅读:
    1052 卖个萌
    编程实现hdfs对文件的操作
    关于Eclipse编译运行MapReduce程序报错问题的解决
    用户模板和用户场景
    用户体验评价
    大二下学期软件工程课程总结
    教师派第二阶段10
    教师派第二阶段09
    教师派第二阶段07
    教师派第二阶段06
  • 原文地址:https://www.cnblogs.com/kaka-qiqi/p/14952968.html
Copyright © 2020-2023  润新知