JAVA-利用反射创建对象,然后将注解的内容设置为对象的值

JAVA-利用反射创建对象,然后将注解的内容设置为对象的值,第1张

分析:
 interface用来声明一个注解﹐格式:public interface注解名{定义内容}√其中的每一个方法实际上是声明了一个配置参数.
 接口用来声明一个注解,格式:公共接口注解名{定义内容}√其中的每一个方法实际上是声明了一个配置参数。
√方法的名称就是参数的名称.
√方法的名称就是参数的名称.
√返回值类型就是参数的类型(返回值只能是基本类型,Class , String , enum ).√可以通过default来声明参数的默认值
√返回值类型就是参数的类型(返回值只能是基本类型,类,字符串,枚举).√可以通过默认来声明参数的默认值
√如果只有一个参数成员,一般参数名为value
√如果只有一个参数成员,一般参数名为值
√注解元素必须要有值﹐我们定义注解元素时,经常使用空字符串,0作为默认值.
√注解元素必须要有值,我们定义注解元素时,经常使用空字符串,0作为默认值.
LoginMethodAnnotation.java
package ght10_annotation;

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

@Target({ElementType.TYPE, ElementType.METHOD})  //目标作用域 只能用于方法
@Retention(RetentionPolicy.RUNTIME) //在哪个地方有效
public @interface LoginMethodAnnotation {
    //注解的参数 : 参数类型 + 参数名()
    String username() default "admin";

    String password() default "admin";

}

myAnnotation.java 

package ght10_annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class myAnnotation {

    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {

        Class c1 = Class.forName("ght10_annotation.Login");   //获取字节码对象
        Field[] fs = c1.getDeclaredFields();    //获取包括父类在内的所有字段
        Method[] ms = c1.getMethods();  //获取该类所有公有方法
        System.out.println("c1 = " + c1);
        Constructor con = c1.getConstructor();  //获取一个对象
        Login l = (Login) con.newInstance();


        for (Method m : ms) {
            if (m.isAnnotationPresent(LoginMethodAnnotation.class)) {
                System.out.println("方法名称: " + m.getName());
                LoginMethodAnnotation lm = m.getAnnotation(LoginMethodAnnotation.class);
                l.setLogin(lm.username(), lm.password());
                System.out.println(l.toString());
            }
        }

    }


}

class Login {   //登录类
    private String username;
    private String password;

    public Login(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public Login() {
    }

    public void setLogin(String username, String password) {
        this.username = username;
        this.password = password;
    }

    @Override
    public String toString() {
        return "Login{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }

    @LoginMethodAnnotation(username = "张三", password = "cqnu123")
    //注解定义参数,利用JAVA反射获取
    public void set(String username, String password) {
        setLogin(username, password);
    }

}


@Target({ElementType.TYPE, ElementType.METHOD})  //目标作用域
//@Retention(RetentionPolicy.RUNTIME) //在哪个地方有效
@interface myAnnotation2 {
    //注解的参数 : 参数类型 + 参数名()
    String name() default "";

    int id() default -1;//如果默认值为-1,代表不存在

}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/759913.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-01
下一篇 2022-05-01

发表评论

登录后才能评论

评论列表(0条)

保存