Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What should be paid attention to when using the Enum type of Java1.5

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "what you need to pay attention to when using the Enum type of Java1.5". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what you need to pay attention to when using the Enum type of Java1.5.

Note:

one. All the enumerated types created are extended to java.lang.Enum. Enum is a new class defined in J2SE 5.0.It is not an enumerated type itself. When creating enumerated types, you must use the enum keyword. You cannot directly define a class that inherits Enum to create an enumerated type, although all enumerated types created are actually subclasses of Enum.

two. Each value defined in an enumerated type is an instance of the enumerated type and is mapped to the Enum (String name, int ordinal) constructor by default. Enumeration types can use parameters to define some of their own constructors.

Two other points to be emphasized:

One is that the constructors of these enumerated types are private. It cannot be called by other classes or other enumerated types. And this private modifier is automatically added by the compiler. If we define these constructors with the public modifier in front of them, it will result in a compilation error.

Second, the variable definition must be after the enumerated type value definition.

three. Each value of the enumeration type is public, static and final. That is, these values are unique and cannot be overridden or modified once defined. And although the static keyword does not appear in each value declaration of the enumerated type, the value is actually static, and we cannot precede the value with the static, public,final modifier

four. When using enumerated types in Switch statements, you must not precede each enumerated type value with the class name of the enumerated type (the value after case), otherwise the compiler will report an error

five. Two new classes, EnumMap and EnumSet, are provided in the java.util package of J2SE 5.0. the combination of these two classes with enumerated types can make previously very cumbersome programs simple and convenient. The EnumMap class provides a special implementation of the java.util.Map interface, where the key is an enumerated type

six. Constant-specific class body:

It is mentioned that enumerated types can define their own functions, but in fact, every value of enumerated types can implement abstract functions defined in enumerated types.

-

Enum Size {

Small (0.8)

Medium (1.0)

Large (1.2)

Private double pricingFactor; / / meets the requirements of 2.2.The variable definition must be after the enumerated type value definition

Size (double p) {

PricingFactor = p

}

Public double getPricingFactor () {

Return pricingFactor

}

}

-

Predefined method of enum

The complete collection of methods (E represents the enumerated type itself):

* public int compareTo (E e)

* public boolean equals (Object o)

* public final ClassgetDeclaringClass ()

* public int hashCode ()

* public String name ()

* public int ordinal ()

* public String toString ()

* public static T valueOf (ClassenumType, String name)

Some methods look familiar, while others are specific to the Enum class.

The compareTo (), equals (), and hashCode () methods are typical Object and Comparable methods, where compareTo () reports the order in which elements are declared. The name () and ordinal () methods return the constructor arguments, while toString () returns the name.

The getDeclaringClass () and valueOf () methods need a little more explanation. The getDeclaringClass () method is similar to Object's getClass () method, but it doesn't have to return the same class. According to the Javadoc of this method: for enum constants with constant-specific class bodies, the value returned by this method may be different from that returned by the Object.getClass () method.

At this point, I believe you have a deeper understanding of "what you need to pay attention to when using the Enum type of Java1.5". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report