Java Enumeration (枚举类型) (1) -- 基本概念
来源:互联网 发布:如何查找iphone 7的mac 编辑:程序博客网 时间:2024/05/05 14:11
1、定义一个功能简单的枚举类型,更定义一个简单的类很相似,例如
public enum Day
{
MONDAY, TUESDAT, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
- 跟类定义一样,枚举类型可以单独放在一个文件里,当一个枚举类型用public修饰时,它对其他包可见,否则只对同一个包中的类可见,这和类定义是一样的。
- 标识符 MONDAY, TUESDAY等就称为枚举常量(enumeration constants)
- 每一个枚举常量被隐式的声明成Day的一个public、static成员,而且其类型为Day,亦就是说这些常量是self-typed的
public enum Day
{
MONDAY, TUESDAT, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY,
}
public enum Day
{
MONDAY, TUESDAT, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY,;
}
public enum Day
{
MONDAY, TUESDAT, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY;
}
3、声明、使用一个枚举类型:
(1)在同一个包中:
package basic;
public class BasicMainClass
{
public static void main(String args[])
{
Day today = Day.SATURDAY;
System.out.println("Today is " + today.toString().toLowerCase());
}
}
(2)在不同包中:
package other;
import basic.Day;
public class OtherMainClass
{
public static void main(String [] args)
{
Day today = Day.SATURDAY;
System.out.println("Today is " + today.toString().toLowerCase());
}
}
4、枚举类型的性质:(摘自o'relly 出版的 Java in A Nutshell 5th)
Enumerated types have no publicconstructor. The only instances of an enumerated type are those declared by the enum.
Enums are not Cloneable, so copies of the existing instances cannot be created.
Enums implement java.io.Serializableso they can be serialized, but the Java serialization mechanism handles them specially to ensure that no new instances are ever created.
Instances of an enumerated type are immutable: each enum value retains its identity. (We'll see later in this chapter that you can add your own fields and methods to an enumerated type, which means that you can create enumerated values that have mutable portions. This is not recommended, but does not affect the basic identity of each value.)
Instances of an enumerated type are stored in public static final fields of the type itself. Because these fields are final, they cannot be overwritten with inappropriate values: you can't assign the DownloadStatus.ERROR value to the DownloadStatus.DONE field, for example.
By convention, the values of enumerated types are written using all capital letters, just as other static final fields are.
Because there is a strictly limited set of distinct enumerated values, it is always safe to compare enum values using the = = operator instead of calling the equals() method.
Enumerated types do have a working equals( ) method, however.The method uses = =finalso that it cannot be overridden.This working equals( ) method allows enumerated values to be used as members of collections such as Set, List, and Map. internally and is
Enumerated types have a working hashCode() method consistent with their equals( )equals(), hashCode( ) is final. It allows enumerated values to be used with classes like java.util.HashMap. method. Like
Enumerated types implement java.lang.Comparable, and the compareTo() method orders enumerated values in the order in which they appear in the enum declaration.
Enumerated types include a working toString( ) method that returns the name of the enumerated value. For example,DownloadStatus.DONE.toString( ) returns the string "DONE" by default.This method is not final, and enum types can provide a custom implementation if they choose.
Enumerated types provide a static valueOf( ) methodthat does the opposite of the default
toString( ) method. For example, DownloadStatus.valueOf("DONE") would return DownloadStatus.DONE.Enumerated types define a final instance method namedordinal()that returns an integer for each enumerated value. The ordinal of an enumerated value represents its position (starting at zero) in the list of value names in the enum declaration. You do not typically need to use the ordinal( ) method, but it is used by a number of enum-related facilities, as described later in the chapter.
Each enumerated type defines a static method namedvalues( ) that returns an array of enumerated values of that type. This array contains the complete set of values, in the order they were declared, and is useful for iterating through the complete set of possible values. Because arrays are mutable, the values( ) method always returns a newly created and initialized array.
Enumerated types are subclasses ofjava.lang.Enum, which is new in Java 5.0. (Enum is not itself an enumerated type.)You cannot produce an enumerated type by manually extending the Enum class, and it is a compilation error to attempt this. The only way to define an enumerated type is with the enum keyword.
It is not possible toextend an enumerated type. Enumerated types are effectively final,but the final keyword is neither required nor permitted in their declarations. Because enums are effectively final,they may not be abstract.
Like classes, enumerated types may implementone or more interfaces.
- Java Enumeration (枚举类型) (1) -- 基本概念
- Java枚举Enumeration接口
- Swift 枚举(enumeration)类型总结
- 变体(Variant)类型枚举 VARENUM enumeration
- C语言的枚举类型(enumeration)
- 【好用的枚举】Enumeration,现在switch支持枚举类型
- 枚举 Enumeration
- 枚举 enumeration
- 枚举enumeration
- 枚举-enumeration
- JAVA中的Enumeration Class(枚举类)
- EA&UML日拱一卒--类图::枚举类型(enumeration)
- Java 获取Enumeration类型的集合方法
- 一个java Enumeration类型的使用方法
- [2014-09-02]JAVA笔记_枚举(Enumeration)
- Java: 枚举(enumeration)和常量的区别
- Java Notes:枚举 (Enumeration)的意义和使用
- 枚举法(Enumeration)
- 【玩转cocos2d-x之二十二】多线程和同步02-售票
- shell中循环Ftp的效率问题
- Java -- IO
- Java使用smb访问远程共享目录
- ConcurrentHashMap
- Java Enumeration (枚举类型) (1) -- 基本概念
- 红帽子认证复习课程-视频分享
- 员工持股占公司股权大部分在5%以下
- LeetCode题解:Reorder List
- Data Blocks
- js框架开发之旅--事件二
- 法133计算机科学课第11周实践题目及参考解答
- [剑指Offer]11.斐波那契数列
- MPMoviePlayerViewController和MPMoviePlayerController的使用