Basic use of Java enumeration classes

Enum type is a new feature in Java 5. It is a new type that allows constants to represent specific data fragments, and all of them are represented in type safe form.

1. Use of constants

At jdk1 Before 5, we defined constants as public static fianl. Now, with enumeration, you can group related constants into an enumeration type, and enumeration provides more methods than constants.

use

Or the use of switch

2. Custom function

use

result:

Red 1Red Name: red index: 1green Name: green index: 2blank Name: white index: 3yello Name: yellow index: 4

Summary:

1. The essence of enumeration is a class. Before enumeration, you can still solve the places where enumeration is needed according to the most basic programming means of Java. Enumeration masks the type information of the enumeration value. Unlike defining variables with public static final, the type must be specified. Enumeration is a template for building constant data structures, which is extensible. The use of enumeration enhances the robustness of the program. For example, when referring to a nonexistent enumeration value, the compiler will report an error. More usages of enumeration need to be studied and created in development. Java 5 and Java 6 have added many new features, and the technology is upgrading. Programmers need to learn if you love Java. Otherwise, if you don't understand the code that others use new features, it's called depression.

2. Enumeration accounts for only a small proportion in the Java family, so I don't use enumeration in many projects. After all, a project is developed and maintained by many people. Using a strange thing will cause reading difficulties for other colleagues. Therefore, constants are mostly defined by public static final.

Reprinted from http://www.cnblogs.com/zhaoyanjun/p/5659811.html

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>