JAVA注解2

休息一下,锻炼了一下身体继续

我们也可以定义自己的Annotation

package cn.yangtao.ceshi;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.CLASS)  ---------------------------->定义该注解的有效区域,还有SOURCE、RUNTIME
public @interface MyAnnotationDemo01 {
 String key();----------------------------------------------->用来接收传递进去的值
 String value();
}

使用MyAnnotationDemo01

package cn.yangtao.ceshi;
public class StudentDemo {
 @SuppressWarnings("deprecation")
 @MyAnnotationDemo01(key="hello",value="hello world")            这行是最关键的,记住赋值的方式
 public static void main(String args[]){
  
  new StudentDemo().sayHello();
 }
 @ Deprecated
 public void sayHello(){
  System.out.println("已经覆写");
 }
}
对于赋值可以有一下几种情况

1、数组型

package cn.yangtao.ceshi;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.CLASS)
public @interface MyAnnotationDemo01 {
 String[] key();
 
}
赋值方式

package cn.yangtao.ceshi;
public class StudentDemo {
 @SuppressWarnings("deprecation")
 @MyAnnotationDemo01(key={"hello","world"})
 public static void main(String args[]){
  
  new StudentDemo().sayHello();
 }
 @ Deprecated
 public void sayHello(){
  System.out.println("已经覆写");
 }
}

2、设置默认型

package cn.yangtao.ceshi;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.CLASS)
public @interface MyAnnotationDemo01 {
 String key() default "hello";
 
}

如果你使用该注解时没有给注解导入属性,则就会默认使用"hello"

3、可以用枚举来限制取值范围

定义枚举类

package cn.yangtao.ceshi;

public enum Color {
 Green,Red,Blue;
}
标明只能给注解传入枚举属性

package cn.yangtao.ceshi;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.CLASS)
public @interface MyAnnotationDemo01 {
 Color key() ;
 
}
使用注解

package cn.yangtao.ceshi;
public class StudentDemo {
 @SuppressWarnings("deprecation")
 @MyAnnotationDemo01(key=Color.Blue) 这个时候就只能取枚举中的对象,如果你有很高的控制欲这一招很好使
 public static void main(String args[]){
  
  new StudentDemo().sayHello();
 }
 @ Deprecated
 public void sayHello(){
  System.out.println("已经覆写");
 }
}

能够设置属性了,要怎么拿到和使用属性呢

答案:反射找到注解所在的方法(如果忘记去看反射)------------>找到该方法上的注解

枚举

package cn.yangtao.ceshi;

public enum Color {
 Green,Red,Blue;
}

注解

package cn.yangtao.ceshi;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)                                         这个地方要设成运行时,否则在取属性的时候会出现空指向异常
public @interface MyAnnotationDemo01 {
 Color key() ;
}

取出useAnnotation方法上注解的属性

package cn.yangtao.ceshi;
public class StudentDemo {
 @SuppressWarnings("deprecation")
 public static void main(String args[]) throws SecurityException, NoSuchMethodException{
  useAnnotation();
  MyAnnotationDemo01 mt=StudentDemo.class.getMethod("useAnnotation", null).
  getAnnotation(MyAnnotationDemo01.class);
  Color c=mt.key();                                                    取出属性的值
  System.out.println(c.name());                                  获得枚举的对象的姓名
 }
 @MyAnnotationDemo01(key=Color.Blue)                  设置该属性的内容
 public  static void useAnnotation() {
  // TODO Auto-generated method stub
  
 }
 
}

 

枚举中元注解

概念:注解的注解就是元注解

常用的有:

@Target   声明被他标注的注解的作用区域比如 类、方法、属性、包等

@Documented   目前不是很清楚

@Inherited       声明该注解可以被继承



老婆要求加的链接>>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值