19.1 基本enum特性

  • enum实例:GROUND, CRAWLING, HANGING;
  • enum类:Shrubbery;
  • Enum类:jdk中的类,自己创建的enum类都继承自Enum类;
package chapter19._1;

enum Shrubbery {GROUND, CRAWLING, HANGING}

public class EnumClass {

    public static void main(String[] args) {
        for (Shrubbery s : Shrubbery.values()) {
            System.out.println(s + " ordinal: " + s.ordinal());
            System.out.print(s.compareTo(Shrubbery.CRAWLING) + " ");
            System.out.print(s.equals(Shrubbery.CRAWLING) + " ");
            System.out.println(s == Shrubbery.CRAWLING);
            System.out.print(s.getDeclaringClass() + " ");
            System.out.println(s.name());
            System.out.println("-------------------------");
        }

        for (String s : "HANGING CRAWLING GROUND".split(" ")) {
            Shrubbery shrub = Enum.valueOf(Shrubbery.class, s);
            System.out.println(shrub);
        }
    }

}

输出:

GROUND ordinal: 0
-1 false false
class chapter19._1.Shrubbery GROUND
-------------------------
CRAWLING ordinal: 1
0 true true
class chapter19._1.Shrubbery CRAWLING
-------------------------
HANGING ordinal: 2
1 false false
class chapter19._1.Shrubbery HANGING
-------------------------
HANGING
CRAWLING
GROUND

你可能感兴趣的:(19.1 基本enum特性)