Java.lang.Enum.ordinal()方法
Java.lang.Enum.ordinal()方法用法实例教程 -
width="728" height="90" frameborder="0" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" allowfullscreen="true" id="aswift_0" name="aswift_0" style="box-sizing: border-box; left: 0px; position: absolute; top: 0px;">
id="cproIframe_u1064372_1" width="728" height="90" src="http://pos.baidu.com/acom?adn=3&at=134&aurl=&cad=1&ccd=24&cec=UTF-8&cfv=17&ch=0&col=zh-CN&conOP=0&cpa=1&dai=1&dis=0&layout_filter=rank%2Ctabcloud<r=https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DABIeJRrmCkRWwP3eGNHmnkublx3rZND4EuxcKDoLQ_DNHCHJD0bQqAc9Ig1_yIgrGo30mZS13U8eo4ypS1HmCK%26ie%3Dutf-8%26f%3D3%26tn%3Dbaidu%26wd%3Dordinal()%26rsp%3D0%26inputT%3D1197<u=http%3A%2F%2Fwww.yiibai.com%2Fjavalang%2Fenum_ordinal.html&lunum=6&n=90029059_cpr&pcs=1077x658&pis=10000x10000&ps=364x297&psr=1440x900&pss=1080x1482&qn=fbd4c32f1cead410&rad=&rsi0=728&rsi1=90&rsi5=4&rss0=%23FFFFFF&rss1=%23FFFFFF&rss2=%230000FF&rss3=%23444444&rss4=%23008000&rss5=&rss6=%23e10900&rss7=&scale=&skin=&td_id=1064372&ti=Java.lang.Enum.ordinal()%E6%96%B9%E6%B3%95&tn=text_default_728_90&tpr=1429519061323&ts=1&version=2.0&xuanting=0&dtm=BAIDU_DUP2_SETJSONADSLOT&dc=2&di=u1064372&tt=1429519061296.29.595.599" align="center,center" marginwidth="0" marginheight="0" scrolling="no" frameborder="0" allowtransparency="true" style="box-sizing: border-box;">
描述
java.lang.Enum.ordinal()方法返回此枚举常量的序数(其枚举声明中的位置,其中初始常量分配的序数为零)。
声明
以下是java.lang.Enum.ordinal()方法的声明
public final int ordinal()
参数
-
NA
返回值
此方法返回的枚举常量的序数。
异常
-
NA
实例
下面的例子说明了如何使用java.lang.Enum.ordinal()方法.
package com.yiibai; import java.lang.*; // enum showing Mobile prices enum Mobile { Samsung(400), Nokia(250),Motorola(325); int price; Mobile(int p) { price = p; } int showPrice() { return price; } } public class EnumDemo { public static void main(String args[]) { System.out.println("CellPhone List:"); for(Mobile m : Mobile.values()) { System.out.println(m + " costs " + m.showPrice() + " dollars"); } Mobile ret = Mobile.Samsung; System.out.println("The ordinal is = " + ret.ordinal()); System.out.println("MobileName = " + ret.name()); } }
让我们来编译和运行上面的程序,这将产生以下结果:
CellPhone List: Samsung costs 400 dollars Nokia costs 250 dollars Motorola costs 325 dollars The ordinal is = 0 MobileName = Samsung