枚举对象:
package test; import java.util.ArrayList; import java.util.List; /** * Created by ASUS on 2016/11/18. */ public enum enumT { TEL("tel", "Telephone", "固定电话"), MOBILE("mobile", "Mobile phone", "手机"), FAX("fax", "Fax", "传真"), MAIL("mail", "Mail", "邮箱"), ZIP("zip", "zip", "邮编"), QQ("QQ", "QQ", "QQ"), WECHAT("WeChat", "WeChat", "微信"), SINA_WEIBO("sina_weibo", "Sina Weibo", "新浪微博"), FACEBOOK("Facebook", "FaceBook", "脸书"), TWITTER("Twitter", "Twitter", "推特"), LINE("Line", "Line", "连我"); /** 编码 */ private String code; /** 描述 */ private String desc; /** 中文名称 */ private String chineseName; /** * 构造函数 * @param code * @param desc * @param chineseName * */ enumT(String code, String desc, String chineseName) { this.code = code; this.desc = desc; this.chineseName = chineseName; } /** * 获取枚举 * * @return 类型 * */ public static List getType() { List type=new ArrayList<> (); for (enumT val : values()) { type.add(val); } return type; } public static List getAll() { List list=new ArrayList<> (); for (enumT val : values()) { ArrayList<String> l=new ArrayList<String>(); l.add(val.getCode()); l.add(val.getChineseName()); l.add(val.getDesc()); list.add(l); } return list; } public String getCode() { return code; } public String getDesc() { return desc; } public String getChineseName() { return chineseName; } } 引用对象:
package test; /** * Created by ASUS on 2016/11/18. */ public class main { public static void main(String args[]){ enumT.QQ.getType(); enumT.QQ.getAll(); } } 运行截图:
getType():
getAll():
解析:
for (enumT val : values()) { type.add(val); } 在for环境中,val已经是enumT对象了(就和enumT.QQ一个样,enumT不能单独调用getCode()等函数,必须要变成enumT.QQ一样才可以调用这些函数)