Java笔记---枚举类和注解
本站字数:108k    本文字数:2.1k    预计阅读时长:9min    访问次数:次
一、枚举类
自定义枚举类
方式一:JDK5.0之前自定义枚举类
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
   | class Seasons {          private  final String SeasonName;     private  final String SeasonDescrp;
           private Seasons(String SeasonName, String SeasonDescrp) {         this.SeasonDescrp = SeasonDescrp;         this.SeasonName   = SeasonName;     }
           public static final Seasons SPRING = new Seasons("SPRING", "春天");     public static final Seasons SUMMER = new Seasons("SUMMER", "夏天");     public static final Seasons AUTUMN = new Seasons("AUTUMN", "秋天");     public static final Seasons WINTER = new Seasons("WINTER", "冬天");
           public String getSeasonName() {         return SeasonName;     }     public String getSeasonDescrp() {         return SeasonDescrp;     }
      @Override     public String toString() {         return "Seasons{" +                 "SeasonName='" + SeasonName + '\'' +                 ", SeasonDescrp='" + SeasonDescrp + '\'' +                 '}';     } }
  | 
 
调用这个类:
1 2 3 4 5 6
   | public class TestEnum {     public static void main(String[] args) {         Seasons spring = Seasons.SPRING; 		System.out.println(spring);     } }
  | 
 
Seasons{ SeasonName=’SPRING’, SeasonDescrp=’春天’}
以上为该自定义枚举类的输出结果
方式二:JDK5.0之后引入关键字enum定义枚举类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
   | enum Season {          SPRING("SPRING", "春天"),     SUMMER("SUMMER", "夏天"),     AUTUMN("AUTUMN", "秋天"),     WINTER("WINTER", "冬天");
                private  final String SeasonName;     private  final String SeasonDescrp;
           private Season(String SeasonName, String SeasonDescrp) {         this.SeasonDescrp = SeasonDescrp;         this.SeasonName   = SeasonName;     } }
  | 
 
那么,枚举类为何没有重写toString()方法呢?
1 2 3 4 5 6 7
   | public class TestEnum {     public static void main(String[] args) {         Season spring = Season.SPRING; 		System.out.println(spring);         System.out.println(Season.class.getSuperClass());     } }
  | 
 
SPRING
class java.lang.Enum
以上呢就是输出结果了!可见,这个类中的toString()方法已经再Enmu类中重写过了
Enum类中的常用方法
- values():返回枚举类型的对象数组。该方法可以很便利的遍历所有枚举值。
 
- valuesOf(String objName):根据提供的objName,返回一个对象名是objName的枚举类型的对象
 
- toString():返回当前常量的名称
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
   | public class TestEnum {     public static void main(String[] args) {         Season spring = Season.SPRING;                   		System.out.println(spring);                           Season[] values = Season.values();         for (Season temp : values) {             System.out.println(temp);         }         System.out.println(Season.class.getSuperClass());                                    Season winter = Season.valueOf("WINTER");         System.out.println(winter);     } }
  | 
 
使用enum关键字定义的枚举类实现接口
情况一:实现接口。再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
   | interface Info {          void show(); } enum Season implements Info {          SPRING("SPRING", "春天"),     SUMMER("SUMMER", "夏天"),     AUTUMN("AUTUMN", "秋天"),     WINTER("WINTER", "冬天");
           private  final String SeasonName;     private  final String SeasonDescrp;
           private Season(String SeasonName, String SeasonDescrp) {         this.SeasonDescrp = SeasonDescrp;         this.SeasonName   = SeasonName;     }
      @Override     public void show() {         System.out.println("这是一个季节");     } }
  | 
 
情况二:让枚举类的对象,分别实现接口中的方法
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
   | interface Info {          void show(); } enum Season implements Info {          SPRING("SPRING", "春天"){         @Override         public void show() { 			         }     },     SUMMER("SUMMER", "夏天") {         @Override         public void show() { }     },     AUTUMN("AUTUMN", "秋天") {         @Override         public void show() { }     },     WINTER("WINTER", "冬天") {         @Override         public void show() { }     };
           private  final String SeasonName;     private  final String SeasonDescrp;
           private Season(String SeasonName, String SeasonDescrp) {         this.SeasonDescrp = SeasonDescrp;         this.SeasonName   = SeasonName;     } }
  | 
 
让每一个对象实现一次接口方法,使得每个对象有不同的动作。
二、注解
	
Annotation是在代码里的特殊标记这些标记可以在编译时,类加载时被读取,并作出相应的处理。不改变原有逻辑的情况下,在程序中添加一些补充信息。
应用
应用一:在生成文档中使用注解(javadoc)
@author  @param @return @version 等等。
应用二:编译时进行格式检查(JDK内置的三个注解)
@Override   表示限定重写父类方法,该注解只能用于方法
@Deprecated  表示所修饰的怨妇已经过时。通常是因为所修饰的结构危险或者存在更好的选择
@SuppressWarnings  抑制编译器警告
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
   | class Person {     public void walk() {         System.out.println("人走路");     } 	     @Deprecated     public void eat() {         System.out.println("人吃饭");     } } interface Info {     void show(); } class Student extends Person implements Info { 	     @Override     public void walk() { System.out.println("学生走路"); }     @Override     public void show() { System.out.println("我是学生"); }          public static main(String[] args) {                                    @SuppressWarnings("unused")         int num = 100;                                    @SuppressWarnings({"unused", "rawtypes"})         ArrayList list = new ArrayList();     } }
  | 
 
应用三:跟踪代码依赖性,实现代替配置文件功能
Servlet3.0提供了注解,使得不需要在web.xml文件中进行Servlet的部署

自定义注解
- 自定义新注解使用 @interface 关键字
 
- 定义 Annotation 成员变量时,以 无参数方法 的形式来声明。
 
- 只有一个参数成员,建议使用value命名
 
- 如果注解没有成员表明是一个标识作用
 
- 使用注解的时候,需要设定一个值,如果已经有了默认值值,那么,就可以不用手写默认值了。
 
- 自定义的注解,需要配上信息处理流程(使用反射)才有意义。
 
- 通常会指明两个元注解,@Retention 和 @Target
 
1 2 3 4 5 6
   | public @interface CustomAnnotation {                    String value() default "Hello"; }
  | 
 
JDK中的元注解
通过反射获取注解信息
JDK8 中注解的新特性
可重复注解
1 2 3 4 5 6 7 8
   |  public @interface MyAnnotations {     MyAnnotation[] value(); } public @interface MyAnnotation {     String[] value(); }
 
 
  | 
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14
   | @MyAnnotations({     @MyAnnotation(value = "hello"),     @MyAnnotation(value = "hi") }) class Person {     public void walk() {         System.out.println("人走路");     } 	     @Deprecated     public void eat() {         System.out.println("人吃饭");     } }
  | 
 
- 在MyAnnotation上声明@Repeatable,成员值为MyAnnotations.class
 
- MyAnnotation的Target和Retention与MyAnnotations相同
 
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
   |  import java.lang.annotation.*;
  @Repeatable(MyAnnotations.class) @Retention(RetentionPolicy.RUNTIME) public @interface MyAnnotation {     String value() default "hello"; }
 
  import java.lang.annotation.*;
  @Retention(RetentionPolicy.RUNTIME) public @interface MyAnnotations {     MyAnnotation[] value(); }
 
  @MyAnnotation("Hello") @MyAnnotation("hi") class Person {     public void walk() {         System.out.println("人走路");     } 	     @Deprecated     public void eat() {         System.out.println("人吃饭");     } }
 
  | 
 
  通过上下的比较,可以发现,JDK8的新特性是对原来注解嵌套的简化。