when we to set
some constants for projects, we always use ‘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 to use
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
firstly,we use the enum to implements operation.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<code>package org.nsg.jdk.testenum;</code>
<code>/**</code>
<code> </code><code>* @description operationtest.java</code>
<code> </code><code>*</code>
<code> </code><code>* @author bysocket</code>
<code> </code><code>* @date 2015-1-8 6:05:59pm</code>
<code> </code><code>* @version 1.0</code>
<code> </code><code>*/</code>
<code>public class operationtest</code>
<code>{</code>
<code> </code><code>public static void main(string[] args)</code>
<code> </code><code>{</code>
<code> </code><code>double x = 2.0,y=4.0;</code>
<code> </code><code>for (operation op : operation.values())</code>
<code> </code><code>system.out.printf("%f %s %f = %f%n", x,op,y,op.apply(x, y));</code>
<code> </code><code>}</code>
<code>}</code>
<code>enum operation</code>
<code> </code><code>plus("+")</code>
<code> </code><code>double apply(double x,double y){return x + y;}</code>
<code> </code><code>},</code>
<code> </code><code>minus("-")</code>
<code> </code><code>double apply(double x,double y){return x - y;}</code>
<code> </code><code>times("*")</code>
<code> </code><code>double apply(double x,double y){return x * y;}</code>
<code> </code><code>divide("/")</code>
<code> </code><code>double apply(double x,double y){return x / y;}</code>
<code> </code><code>};</code>
<code> </code>
<code> </code><code>private final string symbol;</code>
<code> </code><code>operation(string symbol){this.symbol = symbol;}</code>
<code> </code><code>@override</code>
<code> </code><code>public string tostring(){return symbol;}</code>
<code> </code><code>abstract double apply(double x,double y);</code>
run as java application,we can see the console.the result shows operations
<code>2.000000 + 4.000000 = 6.000000</code>
<code>2.000000 - 4.000000 = -2.000000</code>
<code>2.000000 * 4.000000 = 8.000000</code>
<code>2.000000 / 4.000000 = 0.500000</code>
q:‘the enum is just like class?’
a:yep,i think that enum is a nice type.so let us know some methods by apis:
1. firstly,we can
make an 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 has vales.(like string or int …)
3. tostring() method
: returns the name of this enum constant, as contained in the
declaration. this method may be overridden, though it typically isn’t
necessary or desirable. an enum 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 how to use.then learn more and study in depth.’ and in real
projects,we can use enums to replace int or string enum pattern.and enum
is also a typesafe enum.
let us see another example to learn some sets of enum.so lets see it:
47
<code>import java.util.enummap;</code>
<code>import java.util.enumset;</code>
<code>import java.util.iterator;</code>
<code>import java.util.map.entry;</code>
<code> </code><code>* @description weektest.java</code>
<code> </code><code>* @date 2015-1-9 2:55:10pm</code>
<code>public class weektest</code>
<code> </code><code>enumset<</code><code>week</code><code>> weekset = enumset.allof(week.class);</code>
<code> </code><code>system.out.println("enumset:");</code>
<code> </code><code>for (week w : weekset)</code>
<code> </code><code>system.out.println(w);</code>
<code> </code>
<code> </code><code>enummap<</code><code>week</code><code>, string> weekmap = new enummap<</code><code>week</code><code>, string>(week.class);</code>
<code> </code><code>weekmap.put(week.mon, "星期一");</code>
<code> </code><code>weekmap.put(week.tue, "星期二");</code>
<code> </code><code>weekmap.put(week.wed, "星期三");</code>
<code> </code><code>system.out.println("enummap:");</code>
<code> </code><code>for (iterator<</code><code>entry</code><code><week, string>> iterator = weekmap.entryset().iterator(); iterator.hasnext();)</code>
<code> </code><code>{</code>
<code> </code><code>entry<</code><code>week</code><code>, string> weekentry = iterator.next();</code>
<code> </code><code>system.out.println(weekentry.getkey().name()+":"+weekentry.getvalue());</code>
<code> </code><code>}</code>
<code>enum week</code>
<code> </code><code>mon("1"), tue("2"), wed("3"), thu("4"), fri("5"), sat("6"),sun("7");</code>
<code> </code><code>week(string symbol){this.symbol = symbol;}</code>
we can see in console:
<code><</code><code>font</code> <code>size</code><code>=</code><code>"4"</code> <code>face</code><code>=</code><code>"宋體"</code><code>>enumset:</code>
<code>1</code>
<code>2</code>
<code>3</code>
<code>4</code>
<code>5</code>
<code>6</code>
<code>7</code>
<code>enummap:</code>
<code>mon:星期一</code>
<code>tue:星期二</code>
<code>wed:星期三</code>
<code></</code><code>font</code><code>></code>
note: enumset or enummap is easy for we to use.and with them,we can use enums easily.
we use ‘javap -c -private xxx’to know the class:
<code>final class org.nsg.jdk.testenum.week extends java.lang.enum<</code><code>org.nsg.jdk.testenu</code>
<code>m.week> {</code>
<code> </code><code>public static final org.nsg.jdk.testenum.week mon;</code>
<code> </code><code>public static final org.nsg.jdk.testenum.week tue;</code>
<code> </code><code>public static final org.nsg.jdk.testenum.week wed;</code>
<code> </code><code>public static final org.nsg.jdk.testenum.week thu;</code>
<code> </code><code>public static final org.nsg.jdk.testenum.week fri;</code>
<code> </code><code>public static final org.nsg.jdk.testenum.week sat;</code>
<code> </code><code>public static final org.nsg.jdk.testenum.week sun;</code>
<code> </code><code>private final java.lang.string symbol;</code>
<code> </code><code>private static final org.nsg.jdk.testenum.week[] $values;</code>
<code> </code><code>public static org.nsg.jdk.testenum.week[] values();</code>
we can see ‘enum is a class.just is a class.’but no extends.