JAVA学习-注解和反射

注解

元注解

  • @Target : 用于描述注解的使用范围(即注解可以用在什么地方)
  • @Retention : 表示需要什么级别保存该注解信息,用于描述注解的生命周期 SOURCE < CLASS < RUNTIME
  • @Document : 说明该注解将被包含在javadoc中
  • @Inherited : 说明子类可以继承父类中的该注解

自定义注解类

使用@interface自定义注解时,自动继承了java.lang.annotation.Annotation接口

反射

通过反射创建对象

package com.reflection.demo01;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class Demo01 {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Class c1 = Class.forName("com.reflection.user.User");
        //创建对象,必须要有无参构造器!
        Object user = c1.newInstance();
        System.out.println(user);

        //通过有参构造器创建对象
        //Constructor constructor = c1.getConstructor(String.class, int.class, int.class);//获取构造器
        //Object user = constructor.newInstance("tangxiaoyuan", 18, 001);
        //System.out.println(user);

    }
}

通过反射调用方法

package com.reflection.demo04;

import java.lang.reflect.Method;

public class Demo04 {
    public static void main(String[] args) throws Exception {
        Class c1 = Class.forName("com.reflection.user.User");
        Object user = c1.newInstance();
        Method setName = c1.getDeclaredMethod("setName", String.class);
        setName.invoke(user,"shabi");
        System.out.println(user);
    }
}

通过反射调用属性(私有)

package com.reflection.demo02;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class Demo02 {
    public static void main(String[] args) throws Exception {
        //获得Class对象
        Class c1 = Class.forName("com.reflection.user.User");
        Object user = c1.newInstance();//这里必须要有无参构造器!!!
        //通过反射修改属性(私有属性)
        Field age = c1.getDeclaredField("age");
        age.setAccessible(true);//这一步作用是允许修改属性
        age.set(user,19);
        System.out.println(user);

    }
}

反射调用关闭检测节约资源

package com.reflection.demo03;

import com.reflection.user.User;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Demo03 {
    public static void main(String[] args) throws Exception {
        Demo03 demo03 = new Demo03();
        demo03.test01();
        demo03.test02();
        demo03.test03();

    }
    public void test01(){
        //普通调用
        User user = new User();
        long startTime = System.currentTimeMillis();//程序开始时间
        for (int i = 0; i < 100000000; i++) {
            user.getName();
        }
        long endTime = System.currentTimeMillis();//程序结束时间
        System.out.println("普通模式"+(endTime-startTime));
    }
    public void test02() throws ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
        //反射调用
        Class c1 = Class.forName("com.reflection.user.User");
        Object user = c1.newInstance();
        Method getName = c1.getDeclaredMethod("getName", null);
        long startTime = System.currentTimeMillis();//程序开始时间
        for (int i = 0; i < 1000000000; i++) {
           getName.invoke(user,null);
        }
        long endTime = System.currentTimeMillis();//程序结束时间
        System.out.println("反射模式"+(endTime-startTime));
    }
    public void test03() throws Exception {
        //反射调用,关闭检测
        Class c1 = Class.forName("com.reflection.user.User");
        Object user = c1.newInstance();
        Method getName = c1.getDeclaredMethod("getName", null);
        getName.setAccessible(true);//关闭检测
        long startTime = System.currentTimeMillis();//程序开始时间
        for (int i = 0; i < 1000000000; i++) {
            getName.invoke(user,null);
        }
        long endTime = System.currentTimeMillis();//程序结束时间
        System.out.println("关闭检测"+(endTime-startTime));
    }
}

反射获取注解信息

自定义注解

package com.reflection.user;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@txyjava("db_user")
public class User {
    @Fieldtxy(columnName = "db_name", type = "name", len = 10)
    private String name;
    @Fieldtxy(columnName = "db_age", type = "age", len = 10)
    private int age;
    @Fieldtxy(columnName = "db_id", type = "id", len = 10)
    private int id;

    public User() {
    }

    public User(String name, int age, int id) {
        this.name = name;
        this.age = age;
        this.id = id;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }



    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", id=" + id +
                '}';
    }
}
//类名的注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface txyjava{
    String value();

}

//属性的注解
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface  Fieldtxy{
    String columnName();
    String type();
    int len();
}

package com.reflection.demo05;

import com.reflection.user.txyjava;

import java.lang.annotation.Annotation;

public class Demo05 {
    public static void main(String[] args) throws Exception{
        Class c1 = Class.forName("com.reflection.user.User");
        Object user = c1.newInstance();
        //通过反射获得注解
        Annotation[] annotations = c1.getAnnotations();
        for (Annotation annotation : annotations) {
            System.out.println(annotation); //获得了类名的注解
        }

    }
}
////////////////////////////////////////////////////////////////////////////        
        //获得注解value的值
        //Annotation annotation = c1.getAnnotation(txyjava.class);

        //获得类指定的注解
        Field name = c1.getDeclaredField("name");
        Annotation[] annotations1 = name.getAnnotations();
        for (Annotation annotation : annotations1) {
            System.out.println(annotation);
        }

posted @ 2022-03-09 13:59  老哥不老  阅读(30)  评论(0)    收藏  举报