Location>code7788 >text

Basic usage of enumerated types (hands-on)

Popularity:348 ℃/2024-09-26 21:12:28
}
enum Size{SMALL,MEDIUM,LARGE};The analysis and results are as follows: enum Size{SMALL,MEDIUM,LARGE};This code defines the Size enumeration;Size s=;
Size t=;Create Size variables s, t;//s and t refer to the same object?
(st); Compare variables s, t Result: false Indicates that s and t are not referencing the same objectIs // a primitive data type?
(().isPrimitive());Calls the isPrimitive() method in **Class** to determine if the enumerated variable is a primitive data type. Result: false Indicates that the enumerated variable is not a primitive data type. **Added: Primitive data type: refers to data types that store basic data directly (e.g., int, double), they have a fixed number of bytes and are not objects. **//convert from string
Size u=("SMALL");
(s
u); //trueThis code converts a string to an enumerated variable according to the valueOf method of the enumerated type Result: false The same-valued enumerated variable obtained by the string conversion is the same as the originally created enumerated variable.// List all its values
for(Size value:()){
(value);
}`
The results are as follows:
SMALL
MEDIUM
LARGE
The result of running the whole code: