美文网首页
五、反射

五、反射

作者: blank_white | 来源:发表于2020-07-19 20:58 被阅读0次

五、反射

import org.example.domain.Employee;
import org.junit.Test;

import java.lang.reflect.*;
import java.util.Arrays;
import java.util.Objects;

public class RefectTest {

    /**
     *   反射 相关测试
     */
    @Test
    public void t1() throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        Employee employee=new Employee(101,"张三");

        Class c1=employee.getClass();
        // 输出全类名 org.example.domain.Employee
        System.out.println(employee.getClass().getName());

        Class c2=Class.forName("org.example.domain.Employee");

        // 全部输出 true
        System.out.println(c1.equals(c2));
        System.out.println(c1.equals(Employee.class));
        System.out.println(c1==Employee.class);

        // Class ;类实际是一个泛型类 Employee.class 实际上是 Class<Employee>

        // 相当于调用了 Employee 的默认构造器
        Employee e2= (Employee) Class.forName("org.example.domain.Employee").newInstance();


        // 返回包括父类的所有的 public 成员,
       Field[] fields1 = c1.getFields();

        // 返回此类的全部成员
        Field[] fields=c1.getDeclaredFields();

        for (Field f:fields){
            System.out.println("域"+f);
        }

        // 返回所有公有方法 包括从父类继承的
        c1.getMethods();
        // 获得 所有方法,但是不包括从父类继承的
        Method[] methods=c1.getDeclaredMethods();

        for (Method method:methods){
            System.out.println("方法"+method.getName());
        }


        // 返回所有构造器
        c1.getDeclaredConstructors();
        // 返回所有 public 构造器
        Constructor[] constructors = c1.getConstructors();
        for (Constructor constructor:constructors){
            System.out.println("构造器"+constructor);
        }

        System.out.println(c1.getDeclaringClass());

    }


    /**
     * 通过反射获取对象成员 
     */
    @Test
    public void t2() throws NoSuchFieldException, IllegalAccessException {
        Employee e=new Employee(201,"bob");
        Class c1=e.getClass();
        Field f=c1.getDeclaredField("name");
        // 屏蔽 Java 语言的访问检查,使得私有属性也可以被访问
        f.setAccessible(true);
        // 获取 e 对象的 f 域
        Object v=f.get(e);
        // 输出 bob
        System.out.println(v);


    }

    /**
     * 数组 CopyOf 的实现
     * @param o 源数组
     * @param newLength 新数组长度
     * @return 返回新数组
     */
    public static Object goodCopyOf(Object o ,int newLength){

        Class c=o.getClass();
        if (!c.isArray())return null;

        Class componentType=c.getComponentType();
        int length = Array.getLength(o);
        Object newArray = Array.newInstance(componentType, newLength);
        System.arraycopy(o,0,newArray,0,Math.min(length,newLength));

        return newArray;
    }


    /**
     * 利用反射 执行方法
     */
    @Test
    public void t4() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Employee e=new Employee(206,"李四");
        Class c=e.getClass();

        Method m1=c.getDeclaredMethod("setName", String.class);
        Method m2=c.getDeclaredMethod("getName");

        m1.invoke(e,"李白");
        // 输出李白
        System.out.println(m2.invoke(e));



    }

}


相关文章

网友评论

      本文标题:五、反射

      本文链接:https://www.haomeiwen.com/subject/wmlqkktx.html