枚举类型

枚举类型
<wbr style="line-height:25px"><span style="color:#ff6600; line-height:25px">Enum</span><span style="color:#003366; line-height:25px">作为Sun全新引进的一个关键字,看起来很象是特殊的class,它也可以有自己的变量,<br style="line-height:25px"> 可以定义自己的方法,可以实现一个或者多个接口<wbr style="line-height:25px">。</wbr></span><br style="line-height:25px"> 当我们在声明一个enum类型时,我们应该注意到enum类型有如下的一些特征。<br style="line-height:25px"><span style="color:#993300; line-height:25px">1.</span><wbr style="line-height:25px"><span style="color:#000080; line-height:25px">它不能有public的构造函数</span><wbr style="line-height:25px">,这样做可以保证客户代码没有办法新建一个enum的实例。<br style="line-height:25px"><span style="color:#993300; line-height:25px">2.</span><wbr style="line-height:25px"><span style="color:#000080; line-height:25px">所有枚举值都是public,static,final的</span>。注意这一点只是针对于枚举值,我们可以和在普通类里面定义<br style="line-height:25px"> 变量一样定义其它任何类型的非枚举变量,这些变量可以用任何你想用的修饰符。<wbr style="line-height:25px"><br style="line-height:25px"><span style="color:#993300; line-height:25px">3</span>.<wbr style="line-height:25px"><span style="color:#000080; line-height:25px">Enum默认实现了java.lang.Comparable接口<wbr style="line-height:25px">。</wbr></span><br style="line-height:25px"><span style="color:#993300; line-height:25px">4.</span><wbr style="line-height:25px">Enum覆载了toString方法<wbr style="line-height:25px">,因此我们如果调用Color.Blue.toString()默认返回字符串”Blue”.<br style="line-height:25px"><span style="color:#993300; line-height:25px">5.</span><wbr style="line-height:25px"><span style="color:#000080; line-height:25px">Enum提供了一个valueOf方法<wbr style="line-height:25px">,这个方法和toString方法是相对应的。</wbr></span><br style="line-height:25px"> 调用valueOf(“Blue”)将返回Color.Blue.因此我们在自己重写toString方法的时候就要注意到这一点,<br style="line-height:25px"> 一把来说应该相对应地重写valueOf方法。<br style="line-height:25px"><span style="color:#993300; line-height:25px">6.</span><wbr style="line-height:25px"><span style="color:#000080; line-height:25px">Enum还提供了values方法<wbr style="line-height:25px">,这个方法使你能够方便的遍历所有的枚举值</wbr></span>。<br style="line-height:25px"><span style="color:#993300; line-height:25px">7.</span><wbr style="line-height:25px"><span style="color:#000080; line-height:25px">Enum还有一个oridinal的方法<wbr style="line-height:25px">,这个方法返回枚举值在枚举类种的顺序,<br style="line-height:25px"> 这个顺序根据枚举值声明的顺序而定,这里Color.Red.ordinal()返回0。</wbr></span><br style="line-height:25px"><wbr style="line-height:25px"><span style="line-height:25px">注意</span>1:至于Enum的枚举常量的位置(序数)可以用Enum的ordinal()方法得到。<wbr style="line-height:25px"><br style="line-height:25px"> 了解了这些基本特性,我们来看看如何使用它们。<br style="line-height:25px"> 1.<span style="line-height:25px"><wbr style="line-height:25px">遍历所有有枚举值</wbr></span><wbr style="line-height:25px">.知道了有values方法,我们可以轻车熟路地用ForEach循环来遍历了枚举值了。<br style="line-height:25px"> for(Colorc:Color.values())<br style="line-height:25px"> System.out.println(“findvalue:”+c);<br style="line-height:25px"> 2.<span style="line-height:25px"><wbr style="line-height:25px">在enum中定义方法和变量</wbr></span><wbr style="line-height:25px">,比如我们可以为Color增加一个方法随机返回一个颜色。<br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">public</span><span style="color:#ff00ff; line-height:25px">enum</span><span style="color:#993300; line-height:25px"></span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">Color</span><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Red,</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Green,</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Blue;</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">/*<br style="line-height:25px"> *定义一个变量表示枚举值的数目。<br style="line-height:25px"> *(我有点奇怪为什么sun没有给enum直接提供一个size方法).<br style="line-height:25px"> */</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">privatestaticint</span><span style="color:#3366ff; line-height:25px">number=Color.values().length;</span><br style="line-height:25px"><span style="color:#808080; line-height:25px">/**<br style="line-height:25px"> *随机返回一个枚举值<br style="line-height:25px"> @returnarandomenumvalue.<br style="line-height:25px"> */</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicstatic</span><span style="color:#3366ff; line-height:25px">Color</span><span style="color:#ff6600; line-height:25px">getRandomColor</span><span style="color:#3366ff; line-height:25px">(){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">long</span><span style="color:#3366ff; line-height:25px">random=System.currentTimeMillis()%number;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">switch</span><span style="color:#3366ff; line-height:25px">((int)random){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">case</span><span style="color:#3366ff; line-height:25px">0:</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">return</span><span style="color:#3366ff; line-height:25px">Color.Red;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">case</span><span style="color:#3366ff; line-height:25px">1:</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">return</span><span style="color:#3366ff; line-height:25px">Color.Green;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">case</span><span style="color:#3366ff; line-height:25px">2:</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">return</span><span style="color:#3366ff; line-height:25px">Color.Blue;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">default</span><span style="color:#3366ff; line-height:25px">:</span><span style="color:#993300; line-height:25px">return</span><span style="color:#3366ff; line-height:25px">Color.Red;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"> 可以看出这在枚举类型里定义变量和方法和在普通类里面定义方法和变量没有什么区别。<br style="line-height:25px"> 唯一要注意的只是变量和方法定义必须放在所有枚举值定义的后面,否则编译器会给出一个错误。<br style="line-height:25px"> 3.<span style="line-height:25px"><wbr style="line-height:25px">覆载(Override)toString,valueOf方法</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"> 前面我们已经知道enum提供了toString,valueOf等方法,很多时候我们都需要覆载默认的toString方法,那么对于enum我们怎么做呢。<br style="line-height:25px"> 其实这和覆载一个普通class的toString方法没有什么区别。<br style="line-height:25px"> ….<br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">public</span><span style="color:#3366ff; line-height:25px">String</span><span style="color:#ff6600; line-height:25px">toString</span><span style="color:#3366ff; line-height:25px">(){<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">switch</span><span style="color:#3366ff; line-height:25px">(this){<br style="line-height:25px"> caseRed:<br style="line-height:25px"> return"Color.Red";<br style="line-height:25px"> caseGreen:<br style="line-height:25px"> return"Color.Green";<br style="line-height:25px"> caseBlue:<br style="line-height:25px"> return"Color.Blue";<br style="line-height:25px"> default:<br style="line-height:25px"> return"UnknowColor";<br style="line-height:25px"> }<br style="line-height:25px"> }</span><br style="line-height:25px"> ….<br style="line-height:25px"> 这时我们可以看到,此时再用前面的遍历代码打印出来的是<br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Color.Red<br style="line-height:25px"> Color.Green<br style="line-height:25px"> Color.Blue</span><br style="line-height:25px"> 而不是<br style="line-height:25px"><span style="color:#0000ff; line-height:25px">Red<br style="line-height:25px"> Green<br style="line-height:25px"> Blue.</span><br style="line-height:25px"> 可以看到toString确实是被覆载了。一般来说在覆载toString的时候我们同时也应该覆载valueOf方法,以保持它们相互的一致性。<br style="line-height:25px"> 4.<span style="line-height:25px"><wbr style="line-height:25px">使用构造函数<br style="line-height:25px"></wbr></span><wbr style="line-height:25px"><span style="color:#000080; line-height:25px">虽然enum不可以有public的构造函数,但是我们还是可以定义private的构造函数,</span><br style="line-height:25px"> 在enum内部使用。还是用Color这个例子。<br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">public</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff00ff; line-height:25px">enum</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">Color</span><span style="color:#3366ff; line-height:25px">{<br style="line-height:25px"> Red("ThisisRed"),<br style="line-height:25px"> Green("ThisisGreen"),<br style="line-height:25px"> Blue("ThisisBlue");<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">private</span><span style="color:#3366ff; line-height:25px">Stringdesc;<br style="line-height:25px"></span><span style="color:#ff6600; line-height:25px">Color</span><span style="color:#3366ff; line-height:25px">(Stringdesc){<br style="line-height:25px"></span><span style="color:#0000ff; line-height:25px">this.desc=desc;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicString</span><span style="color:#3366ff; line-height:25px">getDesc(){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">return</span><span style="color:#3366ff; line-height:25px">this.desc;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"> 这里我们为每一个颜色提供了一个说明信息,然后定义了一个构造函数接受这个说明信息。<br style="line-height:25px"> 要注意这里构造函数不能为public或者protected,从而保证构造函数只能在内部使用,客户代码不能new一个枚举值的实例出来。<br style="line-height:25px"> 这也是完全符合情理的,因为我们知道枚举值是publicstaticfinal的常量而已。<br style="line-height:25px"> 5.<span style="line-height:25px"><wbr style="line-height:25px">实现特定的接口</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"> 我们已经知道enum可以定义变量和方法,它要实现一个接口也和普通class实现一个接口一样,这里就不作示例了。<br style="line-height:25px"> 6.<span style="line-height:25px"><wbr style="line-height:25px">定义枚举值自己的方法。</wbr></span><wbr style="line-height:25px"><br style="line-height:25px"> 前面我们看到可以为enum定义一些方法,其实我们甚至可以为每一个枚举值定义方法。<br style="line-height:25px"> 这样,我们前面覆载toString的例子可以被改写成这样。<br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">public</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff00ff; line-height:25px">enum</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">Color</span><span style="color:#3366ff; line-height:25px">{<br style="line-height:25px"> Red{<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">public</span><span style="color:#3366ff; line-height:25px">StringtoString(){<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">return</span><span style="color:#3366ff; line-height:25px">"Color.Red";<br style="line-height:25px"> }<br style="line-height:25px"> },<br style="line-height:25px"> Green{<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">public</span><span style="color:#3366ff; line-height:25px">StringtoString(){<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">return</span><span style="color:#3366ff; line-height:25px">"Color.Green";<br style="line-height:25px"> }<br style="line-height:25px"> },<br style="line-height:25px"> Blue{<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">public</span><span style="color:#3366ff; line-height:25px">StringtoString(){<br style="line-height:25px"></span><span style="color:#993300; line-height:25px">return</span><span style="color:#3366ff; line-height:25px">"Color.Blue";<br style="line-height:25px"> }<br style="line-height:25px"> };<br style="line-height:25px"> }</span><br style="line-height:25px"> 从逻辑上来说这样比原先提供一个“全局“的toString方法要清晰一些。<br style="line-height:25px"> 总的来说,enum作为一个全新定义的类型,是希望能够帮助程序员写出的代码更加简单易懂,<br style="line-height:25px"> 个人觉得一般也不需要过多的使用enum的一些高级特性,否则就和简单易懂的初衷想违背了。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">实例1</wbr></span><wbr style="line-height:25px">:<br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicclass</span><span style="color:#ff6600; line-height:25px">Test</span><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><br style="line-height:25px"><span style="color:#808080; line-height:25px">/**<br style="line-height:25px"> *@paramargs<br style="line-height:25px"> */</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">publicstaticvoid</span><span style="color:#3366ff; line-height:25px">main(String[]args){</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">Studentname=Student.ROBIN;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">System.out.println("name:"+name);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">for</span><span style="color:#3366ff; line-height:25px">(Studentst:Student.values())</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">System.out.println(st.name);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff00ff; line-height:25px">enum</span><span style="color:#3366ff; line-height:25px"></span><span style="color:#ff6600; line-height:25px">Student</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">ROBIN("robin"),<br style="line-height:25px"> HARRY("harry",40),<br style="line-height:25px"> ROBBIE("robbie");</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">String</span><span style="color:#3366ff; line-height:25px">name;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">int</span><span style="color:#3366ff; line-height:25px">age;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">private</span><span style="color:#3366ff; line-height:25px">Student(Stringname)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">this</span><span style="color:#3366ff; line-height:25px">(name,0);</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px"></span><span style="color:#993300; line-height:25px">private</span><span style="color:#3366ff; line-height:25px">Student(Stringname,intage)</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">{</span><br style="line-height:25px"><span style="color:#0000ff; line-height:25px">this.name=name;<br style="line-height:25px"> this.age=age;</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span><br style="line-height:25px"><span style="color:#3366ff; line-height:25px">}</span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值