2019-01-22
内容:枚举、注解
一、自定义一个枚举类
1 public class TestSeason { 2 3 public static void main(String[] args) { 4 Season spring = Season.Spring; 5 System.out.println(spring); 6 } 7 } 8 public class Season { 9 //将属性定义为私有常量 10 private final String seasonName; 11 private final String seasonDes; 12 13 //将构造器设置为私有的 14 private Season (String seasonName, String seasonDes) { 15 this.seasonName = seasonName; 16 this.seasonDes = seasonDes; 17 } 18 //利用构造器构造春夏秋冬四个对象 19 public static final Season Spring = new Season("春天","万物复苏"); 20 public static final Season Summer = new Season("夏天","骄阳似火"); 21 public static final Season Fall = new Season("秋天","秋高气爽"); 22 public static final Season Winter = new Season("冬天","白雪皑皑"); 23 //因为属性是常量,所以不能赋值 24 public String getSeasonName() { 25 return seasonName; 26 } 27 public String getSeasonDes() { 28 return seasonDes; 29 } 30 //重写toString方法 31 @Override 32 public String toString() { 33 return "Season [seasonName=" + seasonName + ", seasonDes=" + seasonDes + "]"; 34 } 35 36 }
二、通过关键字enum定义一个枚举类并使用方法
1 public class TestSeason { 2 3 public static void main(String[] args) { 4 Season1 spring = Season1.Spring; 5 System.out.println(spring); 6 //使用values()方法将所有对象存入一个数组中 7 Season1[] season = Season1.values(); 8 for (int i = 0; i < season.length; i++) { 9 System.out.println(season[i]); 10 } 11 //使用valueOf(String name)方法将指定对象名的对象取出来 12 String name = "Winter"; 13 Season1 winter = Season1.valueOf(name); 14 System.out.println(winter); 15 } 16 } 17 public enum Season1 { 18 Spring("春天","万物复苏"), 19 Summer("夏天","骄阳似火"), 20 Fall("秋天","秋高气爽"), 21 Winter("冬天","白雪皑皑"); 22 23 private final String seasonName; 24 private final String seasonDes; 25 26 private Season1 (String seasonName, String seasonDes) { 27 this.seasonName = seasonName; 28 this.seasonDes = seasonDes; 29 } 30 31 public String getSeasonName() { 32 return seasonName; 33 } 34 public String getSeasonDes() { 35 return seasonDes; 36 } 37 38 @Override 39 public String toString() { 40 return "Season1 [seasonName=" + seasonName + ", seasonDes=" + seasonDes + "]"; 41 } 42 43 }
三、让枚举类实现一个接口,并且让枚举类的每个对象都重写接口中的抽象方法
1 public class TestSeason { 2 3 public static void main(String[] args) { 4 Season1 spring = Season1.Spring; 5 System.out.println(spring); 6 //使用values()方法将所有对象存入一个数组中 7 Season1[] season = Season1.values(); 8 for (int i = 0; i < season.length; i++) { 9 System.out.println(season[i]); 10 } 11 //使用valueOf(String name)方法将指定对象名的对象取出来 12 String name = "Winter"; 13 Season1 winter = Season1.valueOf(name); 14 System.out.println(winter); 15 //调用接口中的方法 16 spring.Show(); 17 } 18 } 19 public interface Show { 20 void Show(); 21 } 22 public enum Season1 implements Show { 23 Spring("春天","万物复苏"){ 24 @Override 25 public void Show() { 26 System.out.println("春天:"+Spring.toString()); 27 } 28 }, 29 Summer("夏天","骄阳似火"){ 30 @Override 31 public void Show() { 32 System.out.println("夏天:"+Spring.toString()); 33 } 34 }, 35 Fall("秋天","秋高气爽"){ 36 @Override 37 public void Show() { 38 System.out.println("秋天:"+Spring.toString()); 39 } 40 }, 41 Winter("冬天","白雪皑皑"){ 42 @Override 43 public void Show() { 44 System.out.println("冬天:"+Spring.toString()); 45 } 46 }; 47 48 private final String seasonName; 49 private final String seasonDes; 50 51 private Season1 (String seasonName, String seasonDes) { 52 this.seasonName = seasonName; 53 this.seasonDes = seasonDes; 54 } 55 56 public String getSeasonName() { 57 return seasonName; 58 } 59 public String getSeasonDes() { 60 return seasonDes; 61 } 62 63 @Override 64 public String toString() { 65 return "Season1 [seasonName=" + seasonName + ", seasonDes=" + seasonDes + "]"; 66 } 67 }
四、JDK常用注释
@Override:显示表明方法重写
@Deprecated:表明方法或者类过时,但依旧可以使用
@SuppressWarnings:抑制编译器警告,即不让编译器发出警告
五、自定义注解
使用@interface
六、元注解
释义:修饰注解的注解
@Retention:*用来指定某个Annotation存在的时间长短
*使用格式:@Retention(RetentionPolicy.成员变量)
@Retention(RetentionPolicy.SOURCE)表明该注释在编译时不会保留
@Retention(RetentionPolicy.CLASS)表明在编译时被记录在.class文件中,运行不会保留,这是默认值
@Retention(RetentionPolicy.RUNTIME)表明在编译时被记录在.class文件中,运行时也保留,程序可以通过反射获取该注释
@Target:*用来指定某个Annotation能修饰的地方,默认是所有地方都能修饰
*使用格式@Target(地方),地方可以是FIELD,METHOD,CONSTRUCTION,TYPE,LOCAL_VARIABLE,PACKAGE,PARAMETER,ANNOTATION_TYPE中的任意个
@Documented:*用来指定某个Annotation在生成文档的时候会被保留
*格式:@Documented
@Inherited:*用来指定某个Annotation具有继承性
原文地址:https://www.cnblogs.com/jbrr/p/10305754.html