复习java第五天(枚举、Annotation(注释) 概述)
一、枚举
传统的方式:
1 public class Season { 2 3 //1、因为类的对象是固定的,所以类的属性是常量 4 public final String name; 5 public final String desc; 6 7 //2、因为类的对象是有限个,所以不能在类的外部创建对象,构造器必须私有 8 private Season(String name,String desc){ 9 this.name = name; 10 this.desc = desc; 11 } 12 13 //3、在类的内部创建对象,但要在类的外部能够访问到该对象,而且还不能修改。 14 public static final Season SPRING = new Season("春天","春风又绿江南岸"); 15 public static final Season SUMMER = new Season("夏天","映日荷花别样红"); 16 public static final Season FALL = new Season("秋天","秋水共长天一色"); 17 public static final Season WINTER = new Season("冬天","窗寒西岭千秋雪"); 18 19 public String getName() { 20 return name; 21 } 22 public String getDesc() { 23 return desc; 24 } 25 26 @Override 27 public String toString() { 28 return "Season [name=" + name + ", desc=" + desc + "]"; 29 } 30 }
1 public class TestSeason { 2 3 public static void main(String[] args) { 4 5 Season SPRING = Season.SPRING; 6 System.out.println(SPRING); 7 8 } 9 }
现在的方式:
1 public interface DateInfo { 2 public String getDateInfo(); 3 }
1 public enum Season2 implements DateInfo { 2 3 SPRING("春天","春风又绿江南岸"){ 4 @Override 5 public String getDateInfo() { 6 return "2-5"; 7 } 8 }, 9 SUMMER("夏天","映日荷花别样红"){ 10 @Override 11 public String getDateInfo() { 12 return "5-8"; 13 } 14 }, 15 FALL("秋天","秋水共长天一色"){ 16 @Override 17 public String getDateInfo() { 18 return "8-11"; 19 } 20 }, 21 WINTER("冬天","窗寒西岭千秋雪"){ 22 @Override 23 public String getDateInfo() { 24 return "11-2"; 25 } 26 },; 27 28 private String name; 29 private String desc; 30 31 private Season2(String name,String desc){ 32 this.name = name; 33 this.desc = desc; 34 } 35 36 public String getName() { 37 return name; 38 } 39 40 public String getDesc() { 41 return desc; 42 } 43 44 /* @Override 改为利用匿名内部类重写的方法更酷些 45 public String getDateInfo() { 46 System.out.println(this.toString().equals("SPRING")); 47 return null; 48 } */ 49 }
1 public class TestSeason { 2 3 public static void main(String[] args) { 4 5 // Season SPRING = Season.SPRING; 6 // System.out.println(SPRING); 7 // System.out.println(Season2.SPRING); 8 for(Season2 s:Season2.values()){ 9 // System.out.println(s); 10 // System.out.println(s.toString().equals("SPRING")); 11 System.out.println(s.getDateInfo()); 12 } 13 14 String str = "WINTER"; 15 Season2 s = Season2.valueOf(Season2.class, str); 16 System.out.println(s.getName()+" "+s.getDesc()); 17 } 18 }
二、Annotation(注释)
1、概述:
2、基本的 Annotation:
3、自定义 Annotation:
1 import java.lang.annotation.ElementType; 2 import java.lang.annotation.Target; 3 4 /** 5 * 1. 使用 @interface 定义注解 6 * 2. 使用类似于接口方法声明的方式来定义注解的属性: 其中返回值称为属性的类型, 方法名为属性的名称. 7 * 8 */ 9 @Target(value={ElementType.METHOD, ElementType.TYPE}) 10 public @interface HelloAnnotation { 11 12 public String name() default "atguigu"; 13 14 }
1 public class TestAnnotation { 2 3 @SuppressWarnings("unused") 4 public static void main(String[] args) { 5 6 A a = new A(); 7 a.method2(); 8 9 String str = "abc"; 10 } 11 } 12 13 @HelloAnnotation(name="abc") 14 class A{ 15 16 @HelloAnnotation() 17 void method1(){} 18 19 @Deprecated 20 void method2(){} 21 22 } 23 24 class B extends A{ 25 26 @Override 27 void method1(){} 28 29 }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。