【注解】学习自己写注解【@Retention,决定注解存在的时期,@Target决定注解使用的地方】

Annotation 其实就是代码里的特殊标记, 它用于替代配置文件

1、@SuppressWarnings@deprecated@override的介绍

加了@deprecated注解,当调用这个方法时,会提示过时

加@override注解,是覆盖父类方法

package com.xiaozhi.annotacation;

public class Test {

	@SuppressWarnings("deprecation")
	public static void main(String[] args) {
		System.runFinalizersOnExit(true);
	}
	
	@Deprecated
	public static void sayHello()
	{
		System.out.println("传智播客!");
	}

	@Override
	public String toString() {
		return super.toString();
	}
	
}


2、自己动手写注解,@Retention,决定注解存在的时期,@Target决定注解使用的地方


public @interface MyAnnotation {

}

Test.java

@MyAnnotation
public class Test {

	public static void main(String[] args) {
		if(Test.class.isAnnotationPresent(MyAnnotation.class)){
			MyAnnotation myAnnotation=Test.class.getAnnotation(MyAnnotation.class);
			System.out.println(myAnnotation);
		}
	}
}
运行结果

在自己写的注解前加一个元注解,保证这个注解在运行时仍然存在。

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;


@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {

}

运行结果:


_______________________________________________________________________________________

@Retention元注解,决定注解使用的时期,有三个取值,RetetionPolicy.SOURCE、RetetionPolicy.CLASS、Retetion.RUNTIME,分别对应

java源文件--------------->class文件--------------->内存中的字节码

@override和@SuppressWarnings是在源文件时存在

@Deprecated是在runtime时存在(因为我们需要调用方法才知道方法过时,首先需要把类加载到内存中)

@Target决定注解使用的地方,@Target({ElementType.METHOD , ElementType.TYPE})//这个注解可以加在方法和类上

_______________________________________________________________________________________



3、为注解增加属性,以及使用反射得到注解以及注解内的属性

如果注解中有一个名称为value的属性,且你只想设置value的属性(即其他属性都采用默认值或者只有一个value属性),那么可以省略value=部分。

如果数组属性中只有一个元素,这时候属性值部分可以省略大括号

@Target(value={ElementType.TYPE})可以省略value=部分,由于数组只有一个元素,所以可以省略大括号。简写成@Target(ElementType.TYPE)

MyAnnotation.java

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


@Retention(RetentionPolicy.RUNTIME)
@Target(value={ElementType.TYPE})
public @interface MyAnnotation {
	String color() default"green";
	String value();
	int [] array() default {1,2,3,3};
	Grade grade() default Grade.A;
	MyAnnotation2 annotation();

}

 enum Grade{  
    A("90-100"){  
        public String getScore(){  
            return "优";  
        }  
    },  
    B("80-90"){  
        public String getScore(){  
            return "良";  
        }  
    },  
    C("70-80"){  
        public String getScore(){  
            return "中";  
        }  
    },  
    D("60-70"){  
        public String getScore(){  
            return "差";  
        }  
    };  
    private String value;  
  
    private Grade(String value) {  
        this.value = value;  
    }  
  
    public String getValue() {  
        return value;  
    }  
  
    public void setValue(String value) {  
        this.value = value;  
    }  
      
    public abstract String getScore();  
} 
MyAnnotation2.java

public @interface MyAnnotation2 {

	String value();
}
用Test.java反射出注解的各个属性

@MyAnnotation(color="blue",value="hello",array={1,2,2},grade=Grade.B,annotation=@MyAnnotation2(value="haha"))
public class Test {

	public static void main(String[] args) {
		if(Test.class.isAnnotationPresent(MyAnnotation.class)){
			MyAnnotation myAnnotation=Test.class.getAnnotation(MyAnnotation.class);
			System.out.println(myAnnotation.color());
			System.out.println(myAnnotation.value());
			System.out.println(myAnnotation.array().length);
			System.out.println(myAnnotation.grade());
			System.out.println(myAnnotation.annotation().value());
		}
	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值