Writer:bysocket (mud and brick pulp carpenter)
Micro-Blog: Bysocket
Watercress: Bysocket
Reprint it anywhere u want.
Written in the Font
When we are set some constants for projects, we always use the ' public static final ' to set INT or String constants. Or Sometimes,we can also set the paramters in properties. When the project Starts,we can get the properties of the them. Today,we can use Enum (JDK 1.5).
Three pieces:
1. An Example to Know Enum
2. How to use Enumset and Enummap
3. Enum Analysis
An Example to Know Enum
Firstly,we Use the Enum to implements operation.
?
12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
package org.nsg.jdk.testEnum;
/**
* @Description OperationTest.java
*
* @author BYSocket
* @date 2015-1-8 6:05:59PM
* @version 1.0
*/
public class OperationTest
{
public static void main(String[] args)
{
double x = 2.0,y=4.0;
for (Operation op : Operation.values())
System.out.printf("%f %s %f = %f%n", x,op,y,op.apply(x, y));
}
}
enum Operation
{
PLUS("+")
{
double apply(double x,double y){return x + y;}
},
MINUS("-")
{
double apply(double x,double y){return x - y;}
},
TIMES("*")
{
double apply(double x,double y){return x * y;}
},
DIVIDE("/")
{
double apply(double x,double y){return x / y;}
};
private final String symbol;
Operation(String symbol){this.symbol = symbol;}
@Override
public String toString(){return symbol;}
abstract double apply(double x,double y);
}
|
Run as Java Application,we can see the console.the result shows operations
?
1234 |
2.000000 + 4.000000 = 6.000000 2.000000 - 4.000000 = -2.000000 2.000000 * 4.000000 = 8.000000 2.000000 / 4.000000 = 0.500000 |
Q: ' The enum is a just like class? '
A:yep,i think that Enum was a nice type. So let us know some methods by APIs:
1. Firstly,we can make a abstract method ' apply () ', then set in the Constant-specific class body. Its called constant-specific method implementation.
2. We can make constructor with fields to make the enum have vales. (like String or int ...)
3. ToString () Method:returns The name of this enum constant, as contained in the Declaration. This method is overridden, though it typically isn ' t necessary or desirable. An enum of type should override this method when a more programmer-friendly string form exists.
4. ' Vales () ' Method:to get all enum objects. and ' GetValue () ' can get the Enum object ' value.
Note ' Its easy-to learn. Then learn more and study in depth. ' And in real projects,we can with enums to replace Int or String enum Pattern.and enum is also a typesafe enum.
How to use Enumset and Enummap
Let us see another example to learn some sets of enum.so lets see it:
?
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
package org.nsg.jdk.testEnum;
import java.util.EnumMap;
import java.util.EnumSet;
import java.util.Iterator;
import java.util.Map.Entry;
/**
* @Description WeekTest.java
*
* @author BYSocket
* @date 2015-1-9 2:55:10PM
* @version 1.0
*/
public class WeekTest
{
public static void main(String[] args)
{
EnumSet<
Week
> weekSet = EnumSet.allOf(Week.class);
System.out.println("EnumSet:");
for (Week w : weekSet)
System.out.println(w);
EnumMap<
Week
, String> weekMap = new EnumMap<
Week
, String>(Week.class);
weekMap.put(Week.MON, "星期一");
weekMap.put(Week.TUE, "星期二");
weekMap.put(Week.WED, "星期三");
System.out.println("EnumMap:");
for (Iterator<
Entry
<Week, String>> iterator = weekMap.entrySet().iterator(); iterator.hasNext();)
{
Entry<
Week
, String> weekEntry = iterator.next();
System.out.println(weekEntry.getKey().name()+":"+weekEntry.getValue());
}
}
}
enum Week
{
MON("1"), TUE("2"), WED("3"), THU("4"), FRI("5"), SAT("6"),SUN("7");
private final String symbol;
Week(String symbol){this.symbol = symbol;}
@Override
public String toString(){return symbol;}
}
|
We can see in Console:
?
12345678910111213 |
< font Size = face = "Arial" >enumset : 1 2 3 4 5 6 7 enummap: mon: Monday tue: Tuesday wed: Wednesday </ font > |
Note:enumset or Enummap is easy for we-use. And with Them,we can use enums easily.
Enum Analysis
We use ' javap-c-private xxx ' to know the class:
?
123456789101112131415161718192021 |
final class org.nsg.jdk.testEnum.Week extends java.lang.Enum<
org.nsg.jdk.testEnu
m.Week> {
public static final org.nsg.jdk.testEnum.Week MON;
public static final org.nsg.jdk.testEnum.Week TUE;
public static final org.nsg.jdk.testEnum.Week WED;
public static final org.nsg.jdk.testEnum.Week THU;
public static final org.nsg.jdk.testEnum.Week FRI;
public static final org.nsg.jdk.testEnum.Week SAT;
public static final org.nsg.jdk.testEnum.Week SUN;
private final java.lang.String symbol;
private static final org.nsg.jdk.testEnum.Week[] $VALUES;
public static org.nsg.jdk.testEnum.Week[] values();
|
We can see 'Enum is a class.just are a CLAss. ' But no extends.
Writer:bysocket (mud and brick pulp carpenter)
Micro-Blog: Bysocket
Watercress: Bysocket
Reprint it anywhere u want.
[Java plasterer] Java components 3:java Enum