黑马程序员——高新技术--注解

——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 {1};

    //枚举类型的属性
    EnumTest.TrafficLamp lamp() default EnumTest.TrafficLamp.RED;

    //注解类型的属性
    MetaAnnotation annotationAttr() default @MetaAnnotation( "lhm");

    //Class类型的属性
    Class clazz() default String.class;
}

//
@ItheimaAnnotation(annotationAttr=@MetaAnnotation ("flx" ),color="red",value= "abc",arrayAttr={1,2,3})
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
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值