標籤:字串 rgs 方法 arraylist ceo ace print interface isa
註解類:
1 package com.www.yanwu.Annotation; 2 3 import java.lang.annotation.Documented; 4 import java.lang.annotation.ElementType; 5 import java.lang.annotation.Inherited; 6 import java.lang.annotation.Retention; 7 import java.lang.annotation.RetentionPolicy; 8 import java.lang.annotation.Target; 9 10 11 /** 12 * @ClassName: Demo2 13 * @Description: 自訂註解14 * @author Harvey15 * @date 2017年7月27日 下午9:42:21 16 * 17 */18 @Inherited 19 @Documented 20 @Target(value={ElementType.TYPE,ElementType.METHOD})21 @Retention(RetentionPolicy.RUNTIME)22 public @interface Demo2 {23 String studentName() default "";24 int age() default 0;25 int id() default -1;26 }View Code
@Target註解,作用範圍
@Retention註解可以在定義註解時為編譯器提供註解的保留原則(不填預設CLASS層級)。
CLASS編譯器將把注釋記錄在類檔案中,但在運行時 VM 不需要保留注釋。RUNTIME編譯器將把注釋記錄在類檔案中,在運行時 VM 將保留注釋,因此可以反射性地讀取。SOURCE編譯器要丟棄的注釋。
測試註解類:
1 package com.www.yanwu.Annotation; 2 3 import java.util.ArrayList; 4 import java.util.Date; 5 import java.util.List; 6 /** 7 * @ClassName: Demo1 8 * @Description: 測試 9 * @author Harvey10 * @date 2017年7月27日 下午9:42:21 11 * 12 */13 @Demo2(age=100,id=20,studentName="hello")14 public class Demo1 {15 16 @Override17 public String toString() {18 // TODO Auto-generated method stub19 return super.toString();20 }21 22 @Demo2(age=10,id=2,studentName="harvey")23 public void test001(){24 System.out.println("hello");25 }26 @SuppressWarnings("all")27 public static void main(String[] args) {28 Date d=new Date();29 //d.parse("2017");30 //test001();31 System.out.println(d);32 List list=new ArrayList();33 }34 35 36 }View Code
解析註解:
1 package com.www.yanwu.Annotation; 2 3 4 import java.lang.annotation.Annotation; 5 import java.lang.reflect.Method; 6 7 /** 8 * @ClassName: Demo3 9 * @Demo3: TODO(這裡用一句話描述這個類的作用) 10 * @author Harvey11 * @date 2017年7月27日 下午9:42:58 12 * 13 */14 public class Demo3 {15 public static void test() throws ClassNotFoundException {16 17 }18 19 public static void main(String[] args) throws ClassNotFoundException {20 /* 21 * 1.使用類載入器載入類 22 * Class.forName("類名字串") (注意:類名字串必須是全稱,包名+類名) 23 */ 24 Class c = Class.forName("com.www.yanwu.Annotation.Demo1"); 25 26 //2.判斷類上是否存在註解,並擷取類上面註解的執行個體 27 if(c.isAnnotationPresent(Demo2.class)){ 28 Demo2 d = (Demo2) c.getAnnotation(Demo2.class); 29 System.out.println(d.studentName()); 30 System.out.println(d.age()); 31 System.out.println(d.id()); 32 } 33 34 //3.判斷方法上是否存在註解,並擷取方法上面註解的執行個體 35 System.out.println("===========================");36 Method[] ms = c.getMethods(); 37 for (Method method : ms) { 38 if(method.isAnnotationPresent(Demo2.class)){ 39 Demo2 d = (Demo2)method.getAnnotation(Demo2.class); 40 System.out.println(method.getName());41 System.out.println(d.studentName()); 42 System.out.println(d.age()); 43 System.out.println(d.id()); 44 } 45 } 46 //另一種擷取方法上的註解的解析方法 47 System.out.println("===========================");48 for (Method method : ms) { 49 Annotation[] as = method.getAnnotations(); 50 for (Annotation annotation : as) { 51 if(annotation instanceof Demo2){ 52 System.out.println(((Demo2) annotation).id()); 53 System.out.println(((Demo2) annotation).studentName()); 54 System.out.println(((Demo2) annotation).age()); 55 } 56 } 57 } 58 59 System.out.println("hello");60 }61 62 63 }View Code
Java自訂註解