Java笔记--枚举&注解
1、自定义枚举类的实现,例:
class Season{ //1,提供类的属性,声明为 rivate final private final String name; private final String Desc; //2,声明为 final 的属性,在构造器中初始化,构造器为 private private Season(String name, String desc){ this.name = name; this.Desc = desc; } //3,通过公共的方法来调用属性 public String getName() { return name; }</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getDesc() { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> Desc; } </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">4,创建对象,用public static final来修饰</span> <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">final</span> Season SPRING = <span style="color: rgba(0, 0, 255, 1)">new</span> Season("spring", "春天"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">final</span> Season SUMMER = <span style="color: rgba(0, 0, 255, 1)">new</span> Season("Summer", "夏天"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">final</span> Season AUTUMN = <span style="color: rgba(0, 0, 255, 1)">new</span> Season("Autumn", "秋天"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">final</span> Season WINTER = <span style="color: rgba(0, 0, 255, 1)">new</span> Season("winter", "冬天"<span style="color: rgba(0, 0, 0, 1)">); @Override </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String toString() { </span><span style="color: rgba(0, 0, 255, 1)">return</span> "Season [name=" + name + ", Desc=" + Desc + "]"<span style="color: rgba(0, 0, 0, 1)">; }
}
2、使用 enum 关键字定义枚举类:
public enum Season { SPRING("spring", "春天"), SUMMER("Summer", "夏天"), AUTUMN("Autumn", "秋天"), WINTER("winter", "冬天"); private final String name; private final String desc;</span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getName() { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> name; } </span><span style="color: rgba(0, 0, 255, 1)">public</span><span style="color: rgba(0, 0, 0, 1)"> String getDesc() { </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> desc; } </span><span style="color: rgba(0, 0, 255, 1)">private</span><span style="color: rgba(0, 0, 0, 1)"> Season(String name, String desc){ </span><span style="color: rgba(0, 0, 255, 1)">this</span>.name =<span style="color: rgba(0, 0, 0, 1)"> name; </span><span style="color: rgba(0, 0, 255, 1)">this</span>.desc =<span style="color: rgba(0, 0, 0, 1)"> desc; }
}
3、枚举类常用方法:
1)values();
2)vauueOf(String name);
//1.values() Season[] seasons = Season.values(); for(int i = 0; i < seasons.length; i++){System.out.println(seasons[i]); } //2.valueOf(String name): 传入的参数必须是枚举类存在的对象名字 //否则报 java.lang.IllegalArgumentException 的异常 String str = "SPRING"; Season season = Season.valueOf(str); System.out.println(season);
4、枚举类也可以实现接口,可以让不同的枚举类对象调用被重写的抽象方法,执行效果不同(让每个对象分别重写方法)。
5、元素据(MetaData),就是注解(Annotation),是代码里的特殊标记,这些标记可以在编译,类加载,运行时被读取,并执行相应的处理,通过使用 Annotation,程序员可以在不改变原有逻辑的情况下,在源文件中嵌入一些补充信息。
6、Annotation 可以像修饰符一样被使用,可用于修饰包、类、构造器、方法、成员变量、参数、局部变量的声明、这些信息被保存在 Annotation 的“name=value”对中。
7、Annotation 能被用来为程序元素设置元数据。
8、JDK 中的 3 个基本注解类型:
1)@Override:限定重写父类方法。该注释只能用于方法;
2)@Deprecated: 用于表示某个程序元素(类、方法等)已过时;
3)@SuppressWarnings:抑制编译器警告。
9、自定义 Annotation:
public @interface MyAnnotation {String value() default "MyAnnotation"; }
10、JDK 的元 Annotation 用于修饰其他 Annotation 定义,JDK5.0 提供了专门在注解上的注解类型,分别是:
1)Retention: 只能用于修饰一个 Annotation 定义,用于指定该 Annotation 可以保留多长时间。@Rentention 包含一个 RetentionPolicy 类型的成员变量,使用 @Rentention 时必须为该 value 成员变量指定值(RetentionPolicy.SOURCE、RetentionPolicy.CLASS、RetentionPolicy.RUNTIME)
2)Target:用于修饰 Annotation 定义,用于指定被修饰的 Annotation 能用于修饰哪些程序元素,@Target 也包含一个名为 value 的成员变量。
3)Documented:用于指定被该元 Annotation 修饰的 Annotation 类将被 javadoc 工具提取成文档(定义为 Documented 的注解必须设置 Retention 值为 RUNTIME)。
4)Inherited: 被它修饰的 Annotation 将具有继承性,如果某个类使用了被 @Inherited 修饰的 Annotation,则其子类将自动具有该注解(使用较少)。