In this section, let's talk a little bit about enumeration types.
1. Enum type is a data type
Package Com.ray.ch04;public class Test {public static void main (string[] args) {MyType MyType = Mytype.one; System.out.println (MyType instanceof Object);}} Enum MyType {One, both, three}
Output:
True
2. The enumeration type has rewritten the Tosting method and can be output directly.
Package Com.ray.ch04;public class Test {public static void main (string[] args) {System.out.println (Mytype.one);}} Enum MyType {One, both, three}
Output:
One
3. Have your own unique approach
Package Com.ray.ch04;public class Test {public static void main (string[] args) {for (MyType myType:MyType.values ()) {Sys Tem.out.println (mytype.tostring () + ":" + mytype.ordinal () + "");}}} Enum MyType {One, both, three}
Output:
one:0
Two:1
Three:2
4. Use in conjunction with switch
Package Com.ray.ch04;public class Test {private static int getnum (MyType MyType) {switch (MyType) {case One:return 1;case Two:return 2;case three:return 3;default:return 1;}} public static void Main (string[] args) {System.out.println (Test.getnum (Mytype.one));}} Enum MyType {One, both, three}
Output:
1
Summary: This chapter focuses on the concept and application of enumeration types.
This chapter is here, thank you.
-----------------------------------
Directory
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Understanding java-4.9 Enum types from the beginning