- 浏览: 56229 次
- 性别:
- 来自: 上海
文章分类
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 public constructor. 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.Serializable so 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( ) method that 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 named values( ) 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 of java.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 to extend 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 implement one or more interfaces.
发表评论
-
Java abstract 类
2007-05-20 18:27 1847虽然接触Java已经快一年了,也系统的学过Java语法 ... -
Java 点滴 (1)
2007-05-21 16:30 6801、Java运算符优先级: ... -
Java 移位操作
2007-05-22 17:17 2531移位操作要注意的问题是高(低)位是补0还是补1和对char, ... -
Java Interface
2007-05-26 00:16 22001、一个Interface的方所有法访问权限(visibili ... -
Java 点滴 (2)
2007-07-21 09:55 6181、关于import:(1) 使用 ... -
Java继承
2007-07-22 16:40 9271、关键字super有两个用 ... -
Java点滴 (3)
2007-07-23 15:47 6101、AWT事件继承层次2、常用AWT事件类型列表ActionE ... -
Java Thread
2007-07-31 09:49 9361、中断线程:API: java.lang.Thread(1) ... -
Java 单元测试
2007-08-22 21:00 969由于来自ibm developworks,转载要提交申请,就不 ... -
java 数组
2007-08-28 11:24 6411、数组元素的类型可以是任何原生类型也可以是任何引用类型,特别 ... -
Java Enumeration (枚举类型) (2) -- switch语句
2007-09-08 14:43 1428当枚举类型用在switch语句中时,语法有一点点特别。看例子: ... -
Java Enumeration (枚举类型) (3) -- 自定义类体(class body)
2007-09-08 15:39 1452枚举类型其实是一个有限制的类,很多类的语法都可以用在枚举上面上 ... -
Java Thread (1)
2007-09-12 21:32 10131、开启一个新线程的方法,归结起来不外乎有两种:继承类jav ... -
Java Thread (2)
2007-09-13 16:39 13051、让一个线程sleep有两种方法,一个是直接调用Thread ... -
Java 正则表达式 (1) -- java.util.regex.* 介绍
2007-12-09 19:47 13541、Java 1.4之后的版本引进了一个用于处理正则表达式的包 ...
相关推荐
在Java编程语言中,枚举(Enumeration)是一种特殊的类,用于定义一组固定的常量。它在许多场景下比使用常量或int值更方便,..."Java源码查看枚举类型定义.rar"提供的示例和说明,将帮助你更好地理解和应用这一概念。
枚举(Enumeration)是Java中的一个特殊类类型,用于定义一组常量。本项目"test-enum-demo-master"显然是一个用于演示如何通过反射来操作枚举类的示例。 首先,让我们理解枚举类的基本概念。枚举类在Java中用于定义...
在编程领域,枚举类型(Enumeration Type)是一种强大的工具,用于定义一组预定义的常量。这些常量通常表示特定的值集合,如颜色、星期、状态等。枚举类型可以提供更清晰、更具可读性的代码,同时也能避免因错误的...
本教程重点聚焦于易语言中的“模拟枚举类型”这一特性,旨在帮助你深入理解并掌握这一概念。 在传统编程中,枚举类型(Enumeration)是一种定义一组具有命名常量的数据类型,这些常量通常代表某个特定的值或状态。...
- 所有枚举类型都是`java.lang.Enum`的子类。 2. **方法介绍**: - `values()`:返回包含所有枚举常量的数组。 - `valueOf(String name)`:根据名称获取对应的枚举常量。 - 示例代码: ```java for (Train t ...
- Java集合框架中的迭代器(Iterator)和枚举器(Enumeration)。 - Java中的并发工具类,如ConcurrentHashMap、PriorityQueue等。 - Java中关于锁的机制,包括重入锁、对象锁、类锁等概念。 4. 面向对象编程 -...
Java编程语言中的枚举类(Enumeration Class)是用于表示固定数量的常量集合,它提供了一种更加安全、规范的方式来处理常量。枚举在Java中是一个特殊的类,可以拥有方法和属性,使得代码更加清晰、易读。本教程将...
8. 枚举类型(enumeration type) Java的枚举类型是一种特殊的类,用于定义一组固定的常量,提供了比常量更丰富的功能。 9. 构造器(Constructor) 构造器用于初始化类的新实例,通常与类名相同且没有返回类型。 ...
- 枚举类型是一种特殊的类,表示一组常量。 1.39 Java注解类型 - 注解是一种元数据形式,提供了为程序元素声明信息的途径。 1.40 字节流和字符流 - 字节流用于读写8位字节数据。 - 字符流用于读写16位Unicode字符...
- Java 5 新特性介绍,如泛型、枚举类型等。 - **Chapter 15 Autoboxing/Unboxing, Loop, Enumeration, Varargs, Static Import & Misc**: - 自动装箱/拆箱、枚举类型、可变参数列表、静态导入等语法糖。 - **...
例如,Java 7中switch语句开始支持字符串(String)和枚举(Enumeration),这是对原有只支持基本类型的一个重大改进。 糖块一:switch支持String与枚举 在Java 7之前,switch仅支持基本数据类型,如int、char等。但...
12. **枚举(Enumeration)**:Java中的枚举类型用于定义有限的固定值集合,它是常量的集合,可以防止在程序中不小心修改这些值。 13. **注解(Annotation)**:注解是元数据的一种形式,提供了一种安全地向编译器和JVM...
假设我们有一个枚举类型`Status`,并希望根据某些条件筛选出符合条件的状态: ```java public enum Status { ACTIVE, INACTIVE, PENDING } public class StatusVO { private String name; // getters and ...
一、枚举的基本概念 枚举允许程序员定义一个类型,该类型只包含预定义的一组值。这些值通常被称为枚举成员或枚举常量,它们在程序中具有固定的数值,这些数值通常是整型或字符型。枚举可以提高代码的可读性,减少硬...
9. **Java第九章**:可能涉及枚举类型(Enumeration)、注解(Annotation)以及反射(Reflection),这些都是Java高级特性,枚举用于定义一组固定的值,注解用于提供元数据,反射则允许程序在运行时检查类和接口的信息。...
- **Enumeration**: 枚举,用于表示一组固定的值。 - **Comparable**: 可比较的,用于表示可以相互比较的对象。 - **Thread**: 线程,程序中的最小执行单元。 - **Runnable**: 可运行的,表示一个对象可以被线程执行...
可以使用枚举类型的变量来存储枚举值,例如: ```java Color c = Color.RED; ``` for 语句的简化写法是 Java 语言程序设计中的一个特性,它可以简化 for 循环的编写。例如: ```java String[] array = {"apple", ...
1. 枚举(Enumeration): - 枚举是常量数据类型的集合,本质是数组或集合。 - 它们通常用于定义一组固定的、相关的值,如状态、方向等。 - 在Java中,枚举数据类型是一种复杂数据类型,可以包含方法和属性,提供...
Java 1.5 中引入的枚举类型在 1.6 中继续得到应用,提供了更安全、更易于理解的常量表示方式,支持枚举实例的方法和枚举常量之间的关联。 10. **注解(Annotations)**: 注解是元数据的一种形式,提供了一种安全的...
Java集合框架提供了一整套接口和类来存储和操作对象集合,并且还包括了迭代器(Iterator)、比较器(Comparator)等概念,以及枚举(Enumeration)类型。通过集合框架,程序员可以高效地处理数据集合,而不需要担心...