【Java SE】反射

返回自己和超类的 public 字段、方法、构造器
getFields()
getMethods()
getConstructors() 超类没返回

返回自己的所有 字段、方法、构造器
getDeclaredFields()
getDeclaredMethods()
getDeclaredConstructors()

 

Person 类

class Person {
    public Person() {}
    public Person(String p) {}
    private String personPrivateField = "personPrivateField";
    protected String personProtectedField = "personProtectedField";
    public String personPublicField = "personPublicField";
    private String personPrivateMethod() {
        System.out.println("personPrivateMethod");
        return "personPrivateMethod";
    }
    protected String personProtectedMethod() {
        System.out.println("personProtectedMethod");
        return "personProtectedMethod";
    }
    public String personPublicMethod() {
        System.out.println("personPublicMethod");
        return "personPublicMethod";
    }
}

 

Student 类

class Student extends Person {
    public Student() {}
    public Student(String s) {super(s);}
    private String studentPrivateField = "studentPrivateField";
    protected String studentProtectedField = "studentProtectedField";
    public String studentPublicField = "studentPublicField";
    private String studentPrivateMethod() {
        System.out.println("print: studentPrivateMethod");
        return "studentPrivateMethod";
    }
    private String studentPrivateMethod(String args) {
        System.out.println("print: studentPrivateMethod, " + args);
        return "studentPrivateMethod";
    }
    protected String studentProtectedMethod() {
        System.out.println("print: studentProtectedMethod");
        return "studentProtectedMethod";
    }
    protected String studentProtectedMethod(String args) {
        System.out.println("print: studentProtectedMethod, " + args);
        return "studentProtectedMethod";
    }
    public String studentPublicMethod() {
        System.out.println("print: studentPublicMethod");
        return "studentPublicMethod";
    }
    public String studentPublicMethod(String args) {
        System.out.println("print: studentPublicMethod, " + args);
        return "studentPublicMethod";
    }
}

 

反射方法

package cn.ycx.common.reflect;

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

/**
 * 类
 *
 * @author: ycx
 * @date: 2022-5-21
 * @since 0.0.1
 */
public class YReflect {
    public static void main(String[] args) throws Exception {
        Class c = Class.forName("cn.ycx.common.reflect.Student");
        System.out.println("自己和超类的 字段");
        // 自己和超类的 public 字段
        Field[] fields = c.getFields();
        for (Field item : fields) {
            System.out.println(Modifier.toString(item.getModifiers()) + " " + item.getName());
        }
        System.out.println("自己和超类的 方法");
        // 自己和超类的 public 方法
        Method[] methods = c.getMethods();
        for (Method item : methods) {
            System.out.println(Modifier.toString(item.getModifiers()) + " " + item.getName());
        }
        System.out.println("自己和超类的 构造器");
        // 自己和超类的 Constructor 构造器
        Constructor[] constructors = c.getConstructors();
        for (Constructor item : constructors) {
            System.out.println(Modifier.toString(item.getModifiers()) + " " + item.getName());
        }
        System.out.println("自己的 字段");
        // 自己的 public 字段
        Field[] selfFields = c.getDeclaredFields();
        for (Field item : selfFields) {
            System.out.println(Modifier.toString(item.getModifiers()) + " " + item.getName());
        }
        System.out.println("自己的 方法");
        // 自己的 public 方法
        Method[] selfMethods = c.getDeclaredMethods();
        for (Method item : selfMethods) {
            System.out.println(Modifier.toString(item.getModifiers()) + " " + item.getName());
        }
        System.out.println("自己的 构造器");
        // 自己的 Constructor 构造器
        Constructor[] selfConstructors = c.getDeclaredConstructors();
        for (Constructor item : selfConstructors) {
            System.out.println(Modifier.toString(item.getModifiers()) + " " + item.getName());
        }
    }
}

 

获取字段值,循环查找

Student s = new Student();
Class c = s.getClass();
Field studentPrivateField = null;
Field studentProtectedField = null;
Field studentPublicField = null;
Field[] fields = c.getDeclaredFields();
for (Field field : fields) {
    if ("studentPrivateField".equals(field.getName())) {
        studentPrivateField = field;
    }
    if ("studentProtectedField".equals(field.getName())) {
        studentProtectedField = field;
    }
    if ("studentPublicField".equals(field.getName())) {
        studentPublicField = field;
    }
}
studentPrivateField.setAccessible(true);
System.out.println(studentPrivateField.get(s));
System.out.println(studentProtectedField.get(s));
System.out.println(studentPublicField.get(s));

 

获取字段值,根据名称

Student s = new Student();
Class c = s.getClass();
Field studentPrivateField = c.getDeclaredField("studentPrivateField");
Field studentProtectedField = c.getDeclaredField("studentProtectedField");
Field studentPublicField = c.getDeclaredField("studentPublicField");
studentPrivateField.setAccessible(true);
System.out.println(studentPrivateField.get(s));// 参数是字段所属对象
System.out.println(studentProtectedField.get(s));
System.out.println(studentPublicField.get(s));

 

 

执行方法,循环查找

Method studentPrivateMethod = null;
Method studentProtectedMethod = null;
Method studentPublicMethod = null;
Student s = new Student();
Method[] selfMethods = s.getClass().getDeclaredMethods();// 自己的方法
for (Method item : selfMethods) {
    if ( "studentPrivateMethod".equals(item.getName())) {
        studentPrivateMethod = item;
    }
    if ("studentProtectedMethod".equals(item.getName())) {
        studentProtectedMethod = item;
    }
    if ("studentPublicMethod".equals(item.getName())) {
        studentPublicMethod = item;
    }
}
studentPrivateMethod.setAccessible(true);// 私有方法设置可访问
studentPrivateMethod.invoke(s,null);
studentProtectedMethod.invoke(s, null);
studentPublicMethod.invoke(s, null);

 

执行方法,根据名称

Student s = new Student();
Class c = s.getClass();
Method studentPrivateMethod = c.getDeclaredMethod("studentPrivateMethod", String.class);// 第一个参数方法名,第二参数类型
Method studentProtectedMethod = c.getDeclaredMethod("studentProtectedMethod", String.class);
Method studentPublicMethod = c.getDeclaredMethod("studentPublicMethod", String.class);
studentPrivateMethod.setAccessible(true);// 私有方法设置可访问
studentPrivateMethod.invoke(s,"私有");
studentProtectedMethod.invoke(s, "保护");
studentPublicMethod.invoke(s, "公有");

 

复制任意数组

public static Object arraycopy(Object src, int length) {
    Class componentType = src.getClass().getComponentType();// 源数组类型
    Object dest = Array.newInstance(componentType, length);
    int minLength = Math.min(Array.getLength(src), length);// 长度最短的
    System.arraycopy(src, 0, dest, 0, minLength);
    return dest;
}
public static void main(String[] args) throws Exception {
    Person[] src = { new Person(), new Person() };
    Person[] dest = (Person[]) arraycopy(src, 10);
    System.out.println(dest);
}

 

posted @ 2022-05-22 19:13  翠微  阅读(18)  评论(0编辑  收藏  举报