
package com.itcast.day2;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;//Retention默認為class階段,注解MyAnnotation的生命周期為運行階段@Retention(RetentionPolicy.RUNTIME)//默認值為任何元素,若在數組里只填寫METHOD則使用注解的類編譯報錯@Target({ElementType.METHOD,ElementType.TYPE})public @interface MyAnnotation {}/** * Rentention * 關于java程序的三個階段 * source階段: .java--->編譯--->.class * class階段: .class-->進入jvm檢查階段--->字節碼 * runtime階段: 已經通過安全檢查--被調入內存,被視為字節碼 * */
package com.itcast.day2;@MyAnnotationpublic class MyAnnotationTest {} package com.itcast.day2;public class MyAnnotationTestRun { public static void main(String[] args) { if(MyAnnotationTest.class.isAnnotationPResent(MyAnnotation.class)){ MyAnnotation MyAnnotation=MyAnnotationTest.class.getAnnotation(MyAnnotation.class); System.out.println(MyAnnotation);//com.itcast.day2.MyAnnotation() } }} RententionPolicy.SOURCE、RententionPolicy.CLASS、RententionPolicy.RUNTIME;
分別對應java源代碼—>class文件—>內存中的字節碼
@Override ,用于給編譯器看,是否符合重寫規范(方法名,參數列表,返回值必須相同)。一般是自己寫的代碼.------SOURCE階段
@SuppressWarnings 用于告訴編譯器,不要在編譯階段發出警告。一般是自己寫的代碼.---SOURCE階段
@Deprecated 用于標注方法是否過時。一定是在調入內存后,運行期掃描二進制。有可能是別人寫的我在調用。---RUNTIME階段
|
新聞熱點
疑難解答