Java programming enumeration types! Enumeration types define and override enumeration methods

What is an enumeration type

Enumerated type has long appeared in programming languages. It is used to contain a group of similar values into a type.

The name of this enumeration type is defined as a unique type descriptor, which is similar to the definition of a constant. However, enumeration types can provide a larger range of values for declared variables than constant types.

Simple enumeration type definition

If you want to create a color library, you can define enumeration types like this

enum Color {red,green,yellow,black};

Then you can use it

Custom enumeration type

In C / C + +, we can use enumerated variables like this

Defining values of enumeration types in Java

Then use t. getValue () to get the value of the binding

Override enumeration method

The enumeration class provides us with methods. For details, see enum java

Implementation interface

All enumerations are inherited from Java Lang. enum class.

Because Java does not support multi inheritance, enumerated objects can no longer inherit other classes, but can implement interfaces

Test

Output

summary

The above is all about the enumeration type definition and the method of rewriting enumeration in Java programming. I hope it will be helpful to you. Interested friends can continue to refer to this site: the use of Java programming long data type, the imitation of user login code sharing based on Java programming, etc. if you have any questions, you can leave a message at any time. Xiaobian will reply to you in time. Thank you for your support!

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
分享
二维码
< <上一篇
下一篇>>