枚举: 一:枚举类的作用:
枚举类的理解:类的对象只有有限个,确定的,我们称之为枚举类。
当需要定义一组常量时,强烈建议使用枚举类。
如果枚举类中只有一个对象,则可以作为单例模式的实现方式。
二:定义枚举类:
JDK5.0之前,自定义枚举类;
JDK5.0,增加了可以使用enum 关键字定义枚举类;
枚举类的属性:
枚举类对象的属性不应允许被改动,所以应该使用Private final修饰。
枚举类的使用 private final 修饰的属性应该在构造器中为其赋值。
若枚举类显式的定义了带参数的构造器,则在列出枚举值时也必须对应的传入参数。
自定义枚举类:
私有化类的构造器,保证不能在类的外部创建其对象。
在类的内部创建枚举类的实例。声明为:public static final
对象如果有实例变量,应该声明为private final,并在构造器中初始化。
使用enum定义枚举类:
使用enum定义的枚举类默认继承了java.lang.Enum类,因此不能再继承其它的类。
枚举类的构造器只能使用private 权限修饰符。
枚举类的所有实例必须在枚举类中显示列出(, 分隔 ; 结尾)。列出的实例系统会自动添加public static final修饰 。
必须在枚举类的第一行声明枚举类对象。
Jdk1.5中可以在switch表达式中使用Enum定义的枚举类的对象作为表达式,case子句可以直接使用枚举值的名字,无需添加枚举类作为限定。
三:Enum类中的常用方法:
value();返回枚举类型的对象数组。该方法可以很方便的遍历所有的枚举值。
valueOf(String str);可以把一个字符串转换为对应的枚举类对象。要求字符串必须是枚举类对象的”名字”。如果不是,会有运行时异常:IllegalArgumentException。
toString();返回当前枚举类对象常量的名称。
四:使用enum关键字定义的枚举类实现接口的情况:
实现接口,在enum类中实现抽象方法。
让枚举类的对象分别实现接口中的抽象方法。
Demo: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 public class SeasonTest { public static void main (String[] args) { Season spring = Season.SPRING; System.out.println(spring); } } class Season { private final String seasonName; private final String seasonDesc; private Season (String seasonName,String seasonDesc) { this .seasonName = seasonName; this .seasonDesc = seasonDesc; } 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("冬天" ,"冰天雪地" ); public String getSeasonName () { return seasonName; } public String getSeasonDesc () { return seasonDesc; } @Override public String toString () { return "Season{" + "seasonName='" + seasonName + '\'' + ", seasonDesc='" + seasonDesc + '\'' + '}' ; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 interface Info { void show () ; } enum Season1 implements Info { SPRING("春天" ,"春暖花开" ){ @Override public void show () { System.out.println("春天在哪里?" ); } }, SUMMER("夏天" ,"夏日炎炎" ){ @Override public void show () { System.out.println("宁夏" ); } }, AUTUMN("秋天" ,"秋高气爽" ){ @Override public void show () { System.out.println("秋天不回来" ); } }, WINTER("冬天" ,"冰天雪地" ){ @Override public void show () { System.out.println("大约在冬季" ); } }; private final String seasonName; private final String seasonDesc; private Season1 (String seasonName,String seasonDesc) { this .seasonName = seasonName; this .seasonDesc = seasonDesc; } public String getSeasonName () { return seasonName; } public String getSeasonDesc () { return seasonDesc; } }
注解: 一:注解的使用:
JDK5.0新增的功能。
Annotation 其实就是代码里的特殊标记,这些标记可以在编译、类加载、运行时被读取。并执行相应的处理,通过使用Annotation,程序员可以在不改变原有逻辑的情况下,在源文件中嵌入一些补充信息。
在javaSE中,注解的使用目的比较简单,例如标记过时的功能,忽略警告等。在javaEE/Android中注占据了更重要的角色,例如用来配置应用程序的任何切面,代替javaEE旧版中所遗留的繁冗代码和XML配置等。
二:Annotation的使用示例: ①:生成文档相关的注解。
②:在编译时进行格式检查(JDK内置的三个基本注释)
@Override:限定重写父类的方法,该注解只能用于方法。
@Deprecated:用于表示所修饰的元素(类、方法等)已过时,通常是因为所修饰的结构危险或存在更好的选择。
@SuppressWarnings:抑制编译器警告等。
③:跟踪代码依赖性,实现替代配置文件功能。
常见的Annotation示例:
三:如何自定义注解:
定义新的 Annotation 类型使用 @interface 关键字
自定义注解自动继承了java.lang.annotation.Annotation 接口
Annotation 的成员变量在 Annotation 定义中以无参数方法的形式来声明。其
方法名和返回值定义了该成员的名字和类型。我们称为配置参数。类型只能
是八种基本数据类型、String类型、Class类型、enum类型、Annotation类型、以上所有类型的数组 。
可以在定义 Annotation 的成员变量时为其指定初始值, 指定成员变量的初始
值可使用 default 关键字
如果只有一个参数成员,建议使用参数名为value
如果定义的注解含有配置参数,那么使用时必须指定参数值,除非它有默认
值。格式是“参数名 = 参数值”,如果只有一个参数成员,且名称为value,
可以省略“value=”
没有成员定义的 Annotation 称为标记 ; 包含成员变量的 Annotation 称为元数
据 Annotation
注意: 自定义注解必须配上注解的信息处理流程才有意义。
四:JDK提供的四种元注解: 元注解:对现有的注解进行解释说明的注解。
Retention:指定所修饰的 Annotation 的生命周期:SOURCE\CLASS(默认行为)\RUNTIME 只有声明为RUNTIME生命周期的注解,才能通过反射获取。
Target:用于指定被修饰的 Annotation 能用于修饰哪些程序元素
———-出现的频率较低———-
Documented:表示所修饰的注解在被javadoc解析时,保留下来。
Inherited:被它修饰的 Annotation 将具有继承性。
五:JDK8中注解的新特性:
可重复注解:
① 在MyAnnotation上声明@Repeatable,成员值为MyAnnotations.class
② MyAnnotation的Target和Retention等元注解与MyAnnotations相同。
类型注解:
ElementType.TYPE_PARAMETER 表示该注解能写在类型变量的声明语句中(如:泛型声明)
ElementType.TYPE_USE 表示该注解能写在使用类型的任何语句中。
Demo: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 import org.junit.Test;import java.lang.annotation.Annotation;import java.util.ArrayList;import java.util.Date;public class AnnotationTest { public static void main (String[] args) { Person p = new Student(); p.walk(); Date date = new Date(2020 , 10 , 11 ); System.out.println(date); @SuppressWarnings("unused") int num = 10 ; @SuppressWarnings({ "unused", "rawtypes" }) ArrayList list = new ArrayList(); } @Test public void testGetAnnotation () { Class clazz = Student.class; Annotation[] annotations = clazz.getAnnotations(); for (int i = 0 ;i < annotations.length;i++){ System.out.println(annotations[i]); } } } @MyAnnotation(value="hi") @MyAnnotation(value="abc") class Person { private String name; private int age; public Person () { } @MyAnnotation public Person (String name, int age) { this .name = name; this .age = age; } @MyAnnotation public void walk () { System.out.println("人走路" ); } public void eat () { System.out.println("人吃饭" ); } } interface Info { void show () ; } class Student extends Person implements Info { @Override public void walk () { System.out.println("学生走路" ); } public void show () { } } class Generic <@MyAnnotation T > { public void show () throws @MyAnnotation RuntimeException { ArrayList<@MyAnnotation String> list = new ArrayList<>(); int num = (@MyAnnotation int ) 10L ; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 import java.lang.annotation.*;import static java.lang.annotation.ElementType.*; @Inherited @Repeatable(MyAnnotations.class) @Retention(RetentionPolicy.RUNTIME) @Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE,TYPE_PARAMETER,TYPE_USE}) public @interface MyAnnotation { String value () default "hello" ; } ----------------------------------------------------------------- import java.lang.annotation.Inherited;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;import static java.lang.annotation.ElementType.*; @Inherited @Retention(RetentionPolicy.RUNTIME) @Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE}) public @interface MyAnnotations { MyAnnotation[] value(); }