什么是枚举类
枚举类的理解:类的对象只有有限个,确定的。我们就称此类是枚举类
当需要定义一组常量时,强烈建议使用枚举类
如果枚举类中只有一个对象,则可以做为单例模式的实现方法
自定义枚举类的使用 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 package com.songzx.java;public class Enumer1 { public static void main (String[] args) { Season spring = Season.SPRING; Season summer = Season.SUMMER; Season autumn = Season.AUTUMN; Season winter = Season.WINTER; System.out.println(spring); System.out.println(summer); System.out.println(autumn); System.out.println(winter); System.out.println(spring.getName()); } } class Season { private final String name; private final String desc; private Season (String name,String desc) { this .name = name; this .desc = desc; } 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 getName () { return this .name; } @Override public String toString () { return "Season{" + "name='" + name + '\'' + ", desc='" + desc + '\'' + '}' ; } }
使用 enum 关键字定义枚举类 多个枚举属性之间要用逗号,最后结尾用分号。
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 package com.songzx.java;public class Enumer2 { public static void main (String[] args) { Season1 spring = Season1.SPRING; Season1 autumn = Season1.AUTUMN; System.out.println(Season1.class.getSuperclass()); System.out.println(spring); System.out.println(autumn); } } enum Season1 { AUTUMN("夏天" ,"艳阳高照" ), SPRING("春天" ,"万物复苏" ); private final String name; private final String desc; private Season1 (String name,String desc) { this .name = name; this .desc = desc; } @Override public String toString () { return "Season1{" + "name='" + name + '\'' + ", desc='" + desc + '\'' + '}' ; } }
Enum 类常用方法
toString()
获取对象信息,如果没有重写则返回变量名称
values()
获取枚举类中的所有对象,返回的是一个数组
valueOf(String str)
根据str
找到枚举类中相同名字的对象
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 package com.songzx.java;public class Enumer3 { public static void main (String[] args) { Season1 season1 = Season1.SPRING; System.out.println(season1.toString()); System.out.println("**************" ); Season1[] values = Season1.values(); for (int i = 0 ; i < values.length; i++) { System.out.println(values[i]); } System.out.println("**************" ); System.out.println(Season1.valueOf("AUTUMN" )); } }
使用enum关键字声明的枚举类实现接口的两种方式 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 package com.songzx.java;public class Enumer4 { public static void main (String[] args) { Books run = Books.RUN; System.out.println(run); run.speak(); run.show(); System.out.println("********" ); Books start = Books.START; System.out.println(start); start.speak(); start.show(); System.out.println("********" ); Books stop = Books.STOP; System.out.println(stop); stop.speak(); stop.show(); } } interface Desc { public void show () ; public void speak () ; } enum Books implements Desc { RUN("运行中" ,100 ){ public void speak () { System.out.println("正在路上" ); } }, START("开始" ,299 ){ public void speak () { System.out.println("敌军还有五秒达到战场" ); } }, STOP("停止" ,123 ){ public void speak () { System.out.println("游戏结束" ); } }; private String name; private int time; Books(String name, int time) { this .name = name; this .time = time; } @Override public String toString () { return "Books{" + "name='" + name + '\'' + ", time=" + time + '}' ; } @Override public void show () { System.out.println("使用方式一实现接口方法" ); } }
enum 枚举类简写 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 package com.songzx.java;public class Enumer5 { public static void main (String[] args) { Status end = Status.END; System.out.println(end); System.out.println(Status.FULL); System.out.println(Status.RUN); } } enum Status { FULL,RUN,END; }
什么是注解 注解(Annotation),可以理解为代码里的特殊标记,这些标记可以在编译,类加载,运行时被读取,并执行相应的处理。可以用于修饰包,类,方法,成员变量,参数,局部变量等,这些信息被保存在 Annotation 的 name = value 对中
框架 = 注解 + 反射 + 设计模式
JDK 中内置的三个基本注解
@Override
标记这个方法是重写方法
@Deprecated
标记一个方式已经过时
@SuppressWarnings("unused")
抑制编译器警告信息
@SuppressWarnings
可以接受多个参数, rawtypes
参数可以抑制泛型没定义的警告
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 package com.songzx.java;import java.util.ArrayList;public class Annotation1 { public static void main (String[] args) { Student student = new Student(); student.wisdomTooth(); } } class Person { public void show () { System.out.println("人走路" ); } @Deprecated public void wisdomTooth () { System.out.println("智齿是无用的" ); } } class Student extends Person { @SuppressWarnings("unused") int age = 12 ; @SuppressWarnings({"unused","rawtypes"}) ArrayList arrayList = new ArrayList(); @Override public void show () { super .show(); } }
自定义注解 新建时选择 @Annotation
类型,声明一个 value()
属性
1 2 3 public @interface MyAnnotation { String value () ; }
然后再方法上面可以使用
1 2 3 4 @MyAnnotation(value = "hello") public void show () { System.out.println("Hello" ); }
通过 value = "hello"
为注解定义一个属性名,也可以通过如下方式给注解声明一个默认名
1 2 3 public @interface MyAnnotation { String value () default "word" ; }
元注解 解释:对现有注解进行解释说明的注解
jdk提供四种元注解:
@Retention
指定所修饰类的声明周期,CLASS:默认行为,只有声明为 RUNTIME 注解才通过反射获取
@Documented
@Target
@Inherited
可重复注解 新建 MyAnnotations
1 2 3 4 5 6 7 8 9 10 11 package com.songzx.java;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;import static java.lang.annotation.ElementType.*;@Retention(RetentionPolicy.RUNTIME) @Target({TYPE,FIELD,METHOD,PARAMETER,CONSTRUCTOR,LOCAL_VARIABLE}) public @interface MyAnnotations { MyAnnotation[] value(); }
然后修改 MyAnnotation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 package com.songzx.java;import java.lang.annotation.*;import static java.lang.annotation.ElementType.*;@Repeatable(MyAnnotations.class) @Retention(RetentionPolicy.RUNTIME) @Target({TYPE,FIELD,METHOD,PARAMETER,CONSTRUCTOR,LOCAL_VARIABLE}) public @interface MyAnnotation { String value () default "word" ; }
之后就可以重复使用 MyAnnotation
注解
1 2 3 4 5 6 @MyAnnotation(value = "hello") @MyAnnotation(value = "hello") @Override public void show () { System.out.println("Hello" ); }