天天看點

黑馬程式員——高新技術--注解

——Java教育訓練、Android教育訓練、iOS教育訓練、.Net教育訓練、期待與您交流! ——-

注解:

JDK1.5新特性:注解

注解相當于一種标記

@SuppressWarnings:

在main方法上添加一個注解:@SuppressWarnings(“deprecation”),此注解的作用就是告訴編譯器,雖然我用的方法過時了,但是我還是堅持要用,你就不要再提示了。

@Deprecated:

有時候我們寫的方法被别人調用了,但是如果這個方法有一些bug需要修改,可是如果我們修改了又會影響以前已經調用這個方法的程式。這時候我們可以把這個方法設定為過時,然後重新寫一個方法,這時候就需要用到@Deprecated注解。

@Override:

有時候我們寫的某些方法需要覆寫父類的方法,但是可能方法名或者參數會出現不小心寫錯的情況。這時候就可以為這個方法打上@Override注解,如果有任何差錯,eclipse就會報錯。例如equals方法的參數為Object obj,但是經常會被寫錯,這時候就可以通過@Override注解避免這種情況。

public class AnnotationTest {

       //在main方法上面添加,聲明不要再出現過期提醒資訊
       @SuppressWarnings("deprecation" )
       public static void main(String[] args) {

          //這裡使用了過時的方法,編譯器會出現提醒資訊,但是可以編譯通過
          System. runFinalizersOnExit(true);    

          //聲明該方法已過時
          @Deprecated
          public static void sayHello (){
                System. out.println("hi,heima,I'm coming!" );
          }

          //聲明該方法覆寫了父類
          @override
          public boolean equals(Object obj){
                return false;
          }
      }
}
           

一個注解就是一個類,使用注解,就相當于建立了一個對象。

注解相當于一種标記,在程式中加了注解就等于為程式打上了某種标記,沒加,則等于沒有某種标記。以後,javac編譯器,開發工具和其他程式可以用反射來了解你的類及各種元素上有無何種标記,看你有什麼标記,就去幹相應的事。标記可以加在包,類,字段,方法,方法的參數以及局部變量上。

黑馬程式員——高新技術--注解

注解就相當于一個你的源程式中要調用的一個類,要在源程式中應用某個注解,得先準備好了這個注解類。就像你要調用某個類,得先有開發好這個類。

//定義一個注解類
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.TYPE})
public @interface ItheimaAnnotation {

}

//在類上應用該注解類
@ItheimaAnnotation
    public class AnnotationTest {

        public static void main(String[] args) {

        //判斷類是否應用了某個注解
        if(AnnotationTest.class.isAnnotationPresent(ItheimaAnnotation. class)){

        //通過反射獲得類上的注解對象
        ItheimaAnnotation itheimaAnnotation = AnnotationTest.class .getAnnotation(ItheimaAnnotation. class);
                  System. out.println(itheimaAnnotation);
        }
    }
}
           

元注解就是在注解上添加的注解。

@Retention元注解辨別注解類的生命周期:

有三種取值:RetetionPolicy.SOURCE、RetetionPolicy.CLASS、RetetionPolicy.RUNTIME,分别對應:java源檔案–>class檔案–>記憶體中的位元組碼。

@Target辨別該注解類可以用在哪個地方:

@Target的預設值為任何元素,設定Target等于ElementType.METHOD,原來加在類上的注解就報錯了。

為注解添加屬性:

一個注解相當于一個胸牌,如果你胸前貼了胸牌,就是學校的學生,否則,就不是。如果還想區分出是哪個班的學生,這時候可以為胸牌再增加一個屬性來進行區分。

但是從Java語音規範中得知,對于注解的屬性類型有限制,隻能是:八個原始類型、String類型、Enum類型、Annotation注解類型以及Class類型,除了以上幾個類型之外,為注解添加屬性都會報錯。

如果注解屬性有預設值,那麼可以不标記,否則一定要在被添加的注解上設定該屬性值。

加了屬性的标記效果為:@MyAnnotation(color=”red”)。

import java.lang.annotation.*;
import com.itheima.day1.EnumTest;

public @interface MetaAnnotation {
    String value();
}

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,ElementType.TYPE})
public @interface ItheimaAnnotation {

    //定義基本類型的屬性
    String color();

    //定義基本類型的屬性,并為屬性指定預設值(預設值)
    String value() default "zxx";

    //數組類型的屬性,如果數組屬性中隻有一個元素,可以省略大括号
    int[] arrayAttr() default {};

    //枚舉類型的屬性
    EnumTest.TrafficLamp lamp() default EnumTest.TrafficLamp.RED;

    //注解類型的屬性
    MetaAnnotation annotationAttr() default @MetaAnnotation( "lhm");

    //Class類型的屬性
    Class clazz() default String.class;
}

//
@ItheimaAnnotation([email protected] ("flx" ),color="red",value= "abc",arrayAttr={,,})
public class AnnotationTest {

    public static void main(String[] args) {
        if(AnnotationTest.class.isAnnotationPresent(ItheimaAnnotation.class)){

            //從類中擷取使用到的指定注解類對象
            ItheimaAnnotation itheimaAnnotation = AnnotationTest.class.getAnnotation(ItheimaAnnotation.class);

            //通過注解類對象中擷取屬性值,擷取屬性值時要按照函數的方式擷取
            System. out.println(itheimaAnnotation.color());
            //結果:red
            System. out.println(itheimaAnnotation.arrayAttr().length);
            //結果:3
            System. out.println(itheimaAnnotation.lamp().nextLamp().name());
            //結果:GREEN
            System. out.println(itheimaAnnotation.annotationAttr().value());
            //結果:flx
            System. out.println(itheimaAnnotation.clazz());
             //結果:class java.lang.String
        }
    }
}