软件编程
位置:首页>> 软件编程>> java编程>> 详解java注解相关知识

详解java注解相关知识

作者:红旗下的小兵  发布时间:2022-09-22 12:32:52 

标签:java,注解

定义

 1、如果注解中有属性,那么必须给属性赋值。


package com.lxc.Test;

// 定义一个注解
public @interface Annotation {
   String name(); // 看似name像一个方法,实际上我们把name称为属性
}

使用上边注解:


package com.lxc.Test;

public class Test {
   @Annotation(name="lxc")
   public void test() {
   }
}

详解java注解相关知识

2、如果注解中有属性,且没有定义默认值,那么在使用注解的时候,必须给属性赋值。


public @interface Annotation {
   String name();
   int age();
}

public class Test {
   @Annotation(name="lxc", age=20)
   public void test() {
   }
}

但是注解中如果有默认值,在使用注解时,可以不给属性赋值


public class Test {
   @Annotation(name="lxc")
   public void test() {
   }
}

public @interface Annotation {
   String name();
   String password() default "123";
}

3、value() 属性

如果注解中的一个属性名是value,且有且只有一个value(),在使用注解的时候,属性名可以省略


public class Test {
   @Annotation("lxc")
   public void test() {
   }
}

public @interface Annotation {
   String value();
   String password() default "123";
}

注解中属性的类型有哪些

byte、short、int、float、double、boolean、char、String、Class、枚举

数组:

如果数组属性中有一个元素,那么数组的大括号可以省略:


public @interface Annotation {
   String[] name();
}

public class Test {
   // @Annotation(name={"lxc"}) // 写法一
   @Annotation(name="lxc") // 写法二
   public void test() {
   }
}

枚举:


public enum MyEnum {
   className, name, age,
}

public @interface Annotation {
   String[] name();
   MyEnum[] student();
}

public class Test {
   @Annotation(name="lxc", student = {MyEnum.className, MyEnum.age})
   public void test() {
   }
}

注解如何使用:

(1)标记一个注解只能出现在类或者方法上


@Target(value = {ElementType.TYPE, ElementType.METHOD})
public @interface Annotation {
   String userName();
   MyEnum[] student();
}

(2)标记一个注解可以被反射机制所读取


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

// 标记注解只能出现在类上
@Target(value = {ElementType.TYPE, ElementType.METHOD})
// 标记注解可以被反射机制所读取
@Retention(RetentionPolicy.RUNTIME)
public @interface Annotation {
   String userName();
   MyEnum[] student();
}

获取注解中的属性值

通过反射机制来获取。

(1)获取类上边注解的属性:

注解类:


package com.lxc.Test;

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

// 标记注解只能出现在类上
@Target(value = {ElementType.TYPE, ElementType.METHOD})
// 标记注解可以被反射机制所读取
@Retention(RetentionPolicy.RUNTIME)
public @interface Annotation {
   String userName() default "吕星辰";
}

使用注解类:


// myAnnotation
@Annotation(userName = "哈哈")
public class myAnnotation {
}

获取注解类中 的属性:


package com.lxc.Test;
/**
* c.isAnnotationPresent(注解类.class) : 判断一个类上是否有注解,返回true、false
* c.getAnnotation(注解类.class) : 获取注解类的实例
*
*/
public class Test {
   public static void main(String[] args) throws Exception{
       Class c = Class.forName("com.lxc.Test.myAnnotation");
       System.out.println(c.isAnnotationPresent(Annotation.class));
       // 判断一个类是否有:Annotation这个注解
       if(c.isAnnotationPresent(Annotation.class)) {
           // 获取注解对象
           Annotation ann = (Annotation) c.getAnnotation(Annotation.class);
           // 通过注解对象获取属性值
           System.out.println(ann.userName());
       }
   }
}

详解java注解相关知识

(2)获取类中方法上边注解的属性:

注解类:


package com.lxc.Test;

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

// 标记注解只能出现在类上
@Target(value = {ElementType.TYPE, ElementType.METHOD})
// 标记注解可以被反射机制所读取
@Retention(RetentionPolicy.RUNTIME)
public @interface Annotation {
   String userName();
   String password();
}

在方法上使用注解及获取方法上边的注解:
分析:想获取方法上的注解,首先需要获取该方法,获取该方法的前提,获取该方法的类:


package com.lxc.Test;

import java.lang.reflect.Method;

public class UserAnnotation {
   @Annotation(userName = "lxc", password = "123")
   public void getUser() {}

public static void main(String[] args) throws Exception{
       // 通过反射获取类
       Class c = Class.forName("com.lxc.Test.UserAnnotation");
       // 通过反射获取类中的方法
       Method getUserMethod = c.getDeclaredMethod("getUser");
       // 判断方法是否有 Annotation 这个注解
       if(getUserMethod.isAnnotationPresent(Annotation.class)) {
           Annotation ann = getUserMethod.getAnnotation(Annotation.class);
           System.out.println(ann.userName()); // lxc
           System.out.println(ann.password()); // 123
       }
   }
}

详解java注解相关知识

来源:https://blog.csdn.net/qq_42778001/article/details/118208246

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com