package com.wzy.enumt; public enum Citys { //值为静态常量,每一个常量都是一个实例 BEIJING("北京"), SHANGHAI("上海"), HANGZHOU("杭州"), XIANGGANG("香港"); private String value; Citys(){ System.out.println("构造"); } Citys(String value){ this.value = value; } public String getValue() { return value; } }
public static void main(String[] args) { System.out.println(Citys.BEIJING);//BEIJING System.out.println(Citys.BEIJING.name());//BEIJING System.out.println(Citys.BEIJING.toString());//BEIJING System.out.println(Citys.BEIJING.ordinal());//0位置坐标 System.out.println(Citys.BEIJING.getValue());//北京 System.out.println(Enum.valueOf(Citys.class, "HANGZHOU").getValue());//在Citys.class中查找HANGZHOU属性,并得到值 }
时间: 2024-11-15 06:54:59