jdk5 之前自定义枚举类:
public class Session {
// 枚举类的属性
private final String sessionName;
private final String sessionDesc;
// 私有化构造器
private Session(String sessionName,String sessionDesc){
this.sessionName = sessionName;
this.sessionDesc = sessionDesc;
}
// 创建对象提供值
public final static Session SPRING = new Session("SPRING","春暖花开");
public final static Session SUMMER = new Session("SUMMER","夏日炎炎");
public final static Session AUTUMN = new Session("AUTUMN","秋高气爽");
public final static Session WINTER = new Session("WINTER","冰天雪地");
public String getSessionName() {
return sessionName;
}
public String getSessionDesc() {
return sessionDesc;
}
@Override
public String toString() {
return "Session{" +
"sessionName='" + sessionName + '\'' +
", sessionDesc='" + sessionDesc + '\'' +
'}';
}
jdk5 之后使用 enum 关键自定义枚举类:
enum 枚举类 继承于 java.lang.Enum 类
如果不重写 toString,默认调用 Enum 中的 toString,只输出对象常量
public enum EnumSession {
// 创建对象常量提供值
// 多个对象常量 , 隔开,最后一个对象使用 ;
SPRING ("SPRING","春暖花开"),
SUMMER("SUMMER","夏日炎炎"),
AUTUMN ("AUTUMN","秋高气爽"),
WINTER("WINTER","冰天雪地");
// 枚举类的属性
private final String sessionName;
private final String sessionDesc;
// 私有化构造器
private EnumSession(String sessionName, String sessionDesc){
this.sessionName = sessionName;
this.sessionDesc = sessionDesc;
}
public String getSessionName() {
return sessionName;
}
public String getSessionDesc() {
return sessionDesc;
}
// 不重写,默认是 Enum 类中的 toString,只输出对象常量
@Override
public String toString() {
return "Session{" +
"sessionName='" + sessionName + '\'' +
", sessionDesc='" + sessionDesc + '\'' +
'}';
}
}

常用的方法:
values()方法:返回枚举类型的对象数组。该方法可以很方便地遍历所有的枚举值。valueOf(String str):可以把一个字符串转为对应的枚举类对象。要求字符串必须是枚举类对象的“名字”。如不是,会有运行时异常:IllegalArgumentException。toString():返回当前枚举类对象常量的名称 @Test
public void test() {
// Session spring = Session.SPRING;
// System.out.println(spring);
// toString()
EnumSession winter = EnumSession.WINTER;
System.out.println(winter.toString());
// values();
EnumSession[] values = EnumSession.values();
for (EnumSession value : values) {
System.out.println(value);
}
// valueOf("SPRING") 查找指定对常常量的枚举,如果没有报:java.lang.IllegalArgumentException
EnumSession spring = EnumSession.valueOf("SPRIN2G");
System.out.println(spring);
}
Annotation类型使用**@interface**关键字java.lang.annotation.Annotation**接口Annotation的成员变量在Annotation定义中以无参数方法的形式来声明。其方法名和返回值定义了该成员的名字和类型。我们称为配置参数。类型只能是八种基本数据类型、String类型、Class类型、enum类型、Annotation类型、以上所有类型的数组。Annotation的成员变量时为其指定初始值,指定成员变量的初始值可使用**default**关键字valuevalue,可以省略“value=”Annotation称为标记;包含成员变量的Annotation称为元数据Annotationpublic @interface MyAnnotation {
// 1、如果注解中只有一个属性,可以定义为 value,在使用时可省略: value=
// 2、使用 default 可以设置默认值,有默认值使用注解时可以不传输参数
String value() default "哈哈";
}
使用:
@MyAnnotation
public class AnnotationTest {
}
JDK 的元Annotation 用于修饰其他Annotation 定义
JDK5.0提供了4个标准的meta-annotation类型,分别是:
Retention
Target
Documented
Inherited
@Retention: 只能用于修饰一个Annotation定义, 用于指定该Annotation 的生命周期, @Rentention包含一个RetentionPolicy类型的成员变量, 使用@Rentention时必须为该value 成员变量指定值:
RetentionPolicy.SOURCE:在源文件中有效(即源文件保留),编译器直接丢弃这种策略的注释RetentionPolicy.CLASS:在class文件中有效(即class保留),当运行Java 程序时, JVM 不会保留注解。这是默认值RetentionPolicy.RUNTIME:在运行时有效(即运行时保留),当运行Java 程序时, JVM 会保留注释。程序可以通过反射获取该注释。
@Target: 用于修饰Annotation 定义, 用于指定被修饰的Annotation 能用于修饰哪些程序元素。@Target 也包含一个名为value 的成员变量。
@Retention(RetentionPolicy.RUNTIME) // 运行时有效,可以被反射
@Target(ElementType.FIELD) // 描述该注解可以用在哪些结构上,FIELD : 只能使用在属性上
public @interface MyAnnotation {
// 1、如果注解中只有一个属性,可以定义为 value,在使用时可省略: value=
// 2、使用 default 可以设置默认值,有默认值使用注解时可以不传输参数
String value() default "哈哈";
}
使用:
// @MyAnnotation 错误
public class AnnotationTest {
@MyAnnotation
private String name ;
}
用于指定被该元Annotation 修饰的Annotation 类将被javadoc工具提取成文档。默认情况下,javadoc是不包括注解的。
被它修饰的Annotation 将具有继承性。如果某个类使用了被@Inherited 修饰的Annotation, 则其子类将自动具有该注解。
MyAnnotations :
@Retention(RetentionPolicy.RUNTIME) // 运行时有效,可以被反射
// 描述该注解可以用在哪些结构上
@Target({ElementType.FIELD,ElementType.METHOD,ElementType.CONSTRUCTOR})
public @interface MyAnnotations {
MyAnnotation[] value();
}
MyAnnotation:
@Retention(RetentionPolicy.RUNTIME) // 运行时有效,可以被反射
// 描述该注解可以用在哪些结构上
@Target({ElementType.FIELD,ElementType.METHOD,ElementType.CONSTRUCTOR})
@Repeatable(MyAnnotations.class)
public @interface MyAnnotation {
// 1、如果注解中只有一个属性,可以定义为 value,在使用时可省略: value=
// 2、使用 default 可以设置默认值,有默认值使用注解时可以不传输参数
String value() default "哈哈";
}
使用:
// @MyAnnotation 错误
public class AnnotationTest {
@MyAnnotation
private String name;
@MyAnnotation
@MyAnnotation
public AnnotationTest(String name) {
this.name = name;
}
}
@Target的参数类型ElementType枚举值多了两个:TYPE_PARAMETER,TYPE_USE。ElementType.TYPE_PARAMETER表示该注解能写在类型变量的声明语句中(如:泛型声明)。ElementType.TYPE_USE表示该注解能写在使用类型的任何语句中。