枚举类型遍历

在开发中有时候存在对枚举类型遍历的情况,使用values方法获得数组,所以可以直接通过foreach的方式进行对枚举类型的遍历

package cn.tao.shop;

public class Enum {

    /**
     * @param args
     *            枚举类型
     */
    public static void main(String[] args) {
        User[] users = User.values();
        for (User user : users) {
            user.getNameAndStatus();
        }

    }

}

/**
 * 枚举类型的foreach遍历
 *
 * @author sdt
 *
 */
enum User {

    ADMIN("管理员", 1), CUSTOMER("普通客户", 2), Seller("售货员", 3);
    private String name;
    private Integer status;

    private User(String name, Integer status) {
        this.name = name;
        this.status = status;
    }

    public String getName() {
        return name;
    }

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

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public void getNameAndStatus() {
        System.out.println(this.name + ":" + this.status);
    }

}


你可能感兴趣的:(学习之路)