Java uses the enum keyword to create an enumeration type, which implies that the created types are subclasses of the java.lang.Enum class
Sample code for traversing and switching enums:
/* author by w3cschool.cc Main.java */ enum Car { lamborghini,tata,audi,fiat,honda } public class Main { public static void main(String args[]){ Car c; c = Car.tata; switch(c) { case lamborghini: System.out.println("你选择了 lamborghini!"); break; case tata: System.out.println("你选择了 tata!"); break; case audi: System.out.println("你选择了 audi!"); break; case fiat: System.out.println("你选择了 fiat!"); break; case honda: System.out.println("你选择了 honda!"); break; default: System.out.println("我不知道你的车型。"); break; } } }
The output result of running the above code is:
你选择了 tata!
The above is the content used by the Java example-enum and switch statements. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!