枚举类
1、如何自定义枚举类
当一个类的对象只有有限确定个数时我们称此类为枚举类,当需要定义一组常量时强烈建议用枚举类
JDK5.0之前,自定义枚举类
class Season{
//1.声明Season对象的属性:private final修饰
private final String seasonName;
private final String seasonDesc;
//2.私有化类的构造器,并给对象属性赋值
private Season(String seasonName, String seasonDesc) {
this.seasonName = seasonName;
this.seasonDesc = seasonDesc;
}
//3.提供当前枚举类的多个对象:public static final
public static final Season SPRING=new Season("春天","春暖花开");
public static final Season SUMMER=new Season("夏天","夏日炎炎");
public static final Season AUTUMN=new Season("秋天","秋高气爽");
public static final Season WINTER=new Season("冬天","冰天雪地");
}
2、如何使用关键字enum定义枚举类
JDK5.0之后,可以使用enum关键字定义枚举类
默认继承于class java.lang.Enum
enum Season1{
SPRING("春天","春暖花开"),
SUMMER("夏天","夏日炎炎"),
AUTUMN("秋天","秋高气爽"),
WINTER("冬天","冰天雪地");
private final String seasonName;
private final String seasonDesc;
private Season1(String seasonName, String seasonDesc) {
this.seasonName = seasonName;
this.seasonDesc = seasonDesc;
}
}
3、Enum类的主要方法
values()方法: 返回枚举类型的对象数组,该方法可以很方便地遍历所有的枚举值
valueOf(String str):可以把一个字符串转为对应的枚举类对象。 要求字符面串必须是枚举类对象的“名字”。如不是,会有运行时异常:llegalArgumentException.
toString(): 返回当前枚举类对象常量的名称
package day15;
public class SeasonTest02 {
public static void main(String[] args) {
System.out.println(Season01.AUTUMN.toString());
//valuess()
//values()方法: 返回枚举类型的对象数组,该方法可以很方便地遍历所有的枚举值
Season01[] values = Season01.values();
for (int i = 0; i < values.length; i++) {
System.out.println(values[i]);
}
//Season{seasonName='秋天', seasopnDesc='秋高气爽'}
//Season{seasonName='春天', seasopnDesc='春暖花开'}
//Season{seasonName='夏天', seasopnDesc='夏日炎炎'}
//Season{seasonName='秋天', seasopnDesc='秋高气爽'}
//Season{seasonName='冬天', seasopnDesc='白雪皑皑'}
Thread.State[] values1 = Thread.State.values();
for (int i = 0; i < values1.length; i++) {
System.out.println(values1[i]);
}
//NEW
//RUNNABLE
//BLOCKED
//WAITING
//TIMED_WAITING
//TERMINATED
//valueOf(String str):可以把一个字符串转为对应的枚举类对象。 要求字符面串必须是枚举类对象的“名字”。如不是,会有运行时异常:llegalArgumentException.
Season01 winter = Season01.valueOf("WINTER");
System.out.println(winter);
//Season{seasonName='冬天', seasopnDesc='白雪皑皑'}
}
}
enum Season01{
//3.提供多个对象
SPRING("春天","春暖花开"),
SUMMER("夏天","夏日炎炎"),
AUTUMN("秋天","秋高气爽"),
WINTER("冬天","白雪皑皑");
//2.声明Season对象的属性
private final String seasonName;
private final String seasopnDesc;
private Season01(String seasonName, String seasopnDesc) {
this.seasonName = seasonName;
this.seasopnDesc = seasopnDesc;
}
public String getSeasonName() {
return seasonName;
}
public String getSeasopnDesc() {
return seasopnDesc;
}
@Override
public String toString() {
return "Season{" +
"seasonName='" + seasonName + '\'' +
", seasopnDesc='" + seasopnDesc + '\'' +
'}';
}
}
4、实现接口的枚举类
-
在enum类中实现抽象方法,和一般类相同
-
每一个对象可以重写方法
interface Info{
void show();
}
enum Season1 implements Info{
SPRING("春天","春暖花开"){
@Override
public void show() {
}
},
SUMMER("夏天","夏日炎炎") {
@Override
public void show() {
}
},
AUTUMN("秋天","秋高气爽") {
@Override
public void show() {
}
},
WINTER("冬天","冰天雪地") {
@Override
public void show() {
}
};
private final String seasonName;
private final String seasonDesc;
private Season1(String seasonName, String seasonDesc) {
this.seasonName = seasonName;
this.seasonDesc = seasonDesc;
}
}
注解
1、注解概述:P505
2、常见的Annotation示例
@Overide @Deprecated @SuppressWarnings
3、自定义Annotation
参照SuppressWarnings定义
①注解声明为: @interface
②内部定义成员,通常使用value表示
③可以指定成员的默认值, 使用default定义
④如果自定义注解没有成员,表明是一个标识作用。
自定义注解必须使用反射才有意义
4、JDK中的四种元注解
用于修饰其他注解
Retention:指定所修饰的Annotation的生命周期:SOURCE/CLASS(默认行为)/RUNTIME
只有声明为RUNTIME生命周期的注解才能通过反射获取
Target:用于指定被修饰的Annotation能用于修饰哪些程序元素
------------以下出现频率较低
Documented:表示所修饰的注解在被javadoc解析时保留下来
Inherited:所修饰的注解具有继承性
5、利用反射获取注解信息
6、JDK8中注解的新特性:可重复注解、类型注解
可重复注解:
① 在MyAnnotation上声明@Repeatable,成员值为MyAnnotation.class
② MyAnnotation的Target和Retention等元注解必须和Annotations相同
类型注解:
➢ElementType.TYPE_PARAMETER表示该注解能写在类型变量的声明语句中(如:泛型声明)
➢ElementType.TYPE_USE表示该注解能写在使用类型的任何语句中。