首页 > 代码库 > Enum的基本使用

Enum的基本使用

package enum_test;public enum Shrubbery {    GROUND, CRAWLING, HANGING}
package enum_test;public class EnumClass {    public static void main(String[] args) {        System.out.println(Shrubbery.class);        // 枚举转成数组        System.out.println(Shrubbery.values());        // 遍历枚举的元素        for (Shrubbery e : Shrubbery.values()) {            System.out.println(e);            // 元素的位置            System.out.println(e + ".ordinal():\t" + e.ordinal());            // 比较元素位置            System.out.println(e + ".compareTo(Shrubbery.CRAWLING):\t" + e.compareTo(Shrubbery.CRAWLING));            // equals            System.out.println(e + ".equals(Shrubbery.GROUND):\t" + e.equals(Shrubbery.GROUND));            // ==            System.out.println(e + " == (Shrubbery.GROUND):\t" + (e == Shrubbery.CRAWLING));            // 结果等同于.class            System.out.println(e + ".getDeclaringClass():\t" + e.getDeclaringClass());            System.out.println(e + ".name():\t" + e.name());            System.out.println("---------------------");        }    }}

 

Enum的基本使用