There are many new features in Jsdk5.0, such as generics, enhanced loops, improved loading, unboxing, static introduction, and so on, which greatly enhances the ease of use of the Java language.
I am now doing a project from the COM + (DCOM) platform to the Java EE platform for porting there are many definitions of enumerated types, which used to be introduced into the third party class library, or write the class library themselves, or simply defined as static variables to use, it is inconvenient, and may cause a lot of problems. The release of Jsdk5.0 just solves these problems.
Here is a small example of the application of enumerations, which is believed to make it easier to understand the use of enumerations in Java.
A normal, value-free enumeration type can be defined as the following form:
Private enum mycolor{Red, yellow, Blue}
But when we use the value in many cases, the following test class is more capable of describing the powerful features of the enumerated types in Java, the following code:
Class Enumtest
{
Private enum mycolor//defines an MyColor enumeration type
{
Red (1),//defines a member of the enumeration type
Yellow (2),
Blue (3);
Private final int num;//storage attribute, because the ordinal number is usually relatively stable do not want to be changed, so declare into final
MyColor (int num)//similar to the initialization of the class constructor, which automatically loops to the member assignment
{
This.num = num;
}
private int Getnum ()//Get the number of the corresponding Member
{
return num;
}
}
public void Showcolor ()//exposed to external access interface
{
For (MyColor mycolor:MyColor.values ())//The new enhanced loop mode used here
if (Scolor.getnum () ==3)//Here is the serial number comparison
{
System.out.println ("true");
}
}
public static void Main (String args[])//Test our Class
{
Enumtest test = new Enumtest ();
Test.showcolor ();
}
}
At the command line, enter:
Javac-source 1.5 Enumtest.java
Note: Some 5.0 compilers default to 1.4.2 as default, so the new attribute is unrecognized, so adding –source 1.5 to the compilation command line specifies that the new attribute be used.
Compile the file, and then enter
Java enumtest
The results of the operation are:
Red = 1
Yellow = 2
Blue = 3
Blue number is: 3
True
For members of an enumeration type, we can specify not only ordinal numbers, but also multiple properties, and many methods can be written, because the enumerations in Java are object-oriented and a real class, and the enumeration in Java is a class operation. Examples of documents in the SDK belt:
public enum Planet {
MERCURY (3.303e+23, 2.4397e6),
VENUS (4.869e+24, 6.0518e6),
Earth (5.976e+24, 6.37814e6),
MARS (6.421e+23, 3.3972e6),
JUPITER (1.9e+27, 7.1492e7),
SATURN (5.688e+26, 6.0268e7),
Uranus (8.686E+25, 2.5559E7),
NEPTUNE (1.024e+26, 2.4746e7),
PLUTO (1.27e+22, 1.137e6);
Private final double mass; In kilograms
Private final double radius; In meters
Planet (double mass, double radius) {
This.mass = mass;
This.radius = radius;
}
Private double Mass () {return mass;}
Private double radius () {return radius;}
Universal gravitational constant (M3 kg-1 s-2)
public static final Double G = 6.67300E-11;
Double surfacegravity () {
return G * Mass/(RADIUS * radius);
}
Double Surfaceweight (double othermass) {
Return Othermass * surfacegravity ();
}
}
Refer to the $jdk-1_5_0-doc$\docs\guide\language\enums.html in the SDK documentation for details.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.