Java基础(七)---- 反射 reflect

来源:互联网 发布:资海网络集团 编辑:程序博客网 时间:2024/06/08 10:59
package com.test;import java.lang.reflect.Constructor;import java.lang.reflect.Field;import java.lang.reflect.Method;public class ReflectTest {    public static void main(String[] args)throws Exception {        // 反射包括: field、 constructor 、 method        String str = "abcdefg";        Class cls1 = str.getClass(); // 获取类的字节码,在内存中仅存一份        Class cls2 = String.class;        Class cls3 = Class.forName("java.lang.String");        System.out.println(cls1 == cls2);        System.out.println(cls1 == cls3);        System.out.println(cls1.isPrimitive()); // 判断是否是原始类型 9类 boolean byte char short int long float double void        System.out.println(int.class.isPrimitive());        System.out.println(String[].class.isPrimitive());        System.out.println(void.class.isPrimitive());        System.out.println(boolean[].class.isArray()); // 判断是否是数组        /*------------------------------------- 构造函数 (代码块中的构造函数,只有一份) ---------------------------------------*/        /*Constructor<Test> constructor = Test.class.getConstructor(int.class, String.class);        System.out.println(11111constructor.getModifiers());        Test test = constructor.newInstance(10, "zhangsan");        Constructor<Test> constructor2 = (Constructor<Test>) Class.forName("com.test.Test").getConstructor(int.class);        Test test1 = constructor2.newInstance(10);        test1.method4();        Constructor<Test> constructor3 = Test.class.getDeclaredConstructor(byte[].class);        // 值为true 则指示反射的对象在使用时应该取消java语言访问检查。值为false,则只是反射的对象应该实施java语言检查        // 当所要反射的对象为私有或其他访问修饰符时,需要用declared方法,并且需要设置accessible        constructor3.setAccessible(true);        System.out.println(constructor3.getModifiers());        // 构建不带参数的反射        Object test3 = Class.forName("com.test.Test").newInstance();*/        /*------------------------------------- 成员变量  (代码块中的成员变量,只有一份) ---------------------------------------*/        /*Test test = new Test(10, "张三", "江苏省南京市");        // f1 不是对象上的变量,而是类上的,要取对象上的值        Field f1 = test.getClass().getField("address");        // 获取对象上的成员变量的值        System.out.println(f1.get(test));        // 设置成员变量的值        f1.set(test, "2222");        System.out.println(f1.getType());        System.out.println(f1.getModifiers());        // 先获取定义的字段,然后要访问实际对象时,要先设置accessible属性        Field f2 = test.getClass().getDeclaredField("name");        f2.setAccessible(true);        System.out.println(f2.get(test));*/        /*------------------------------------- 成员方法(代码块中的成员方法,只有一份)  ---------------------------------------*/        Test test = new Test(10, "张三", "江苏省南京市");        Method m1 = test.getClass().getMethod("method5", String.class, String.class);        m1.setAccessible(true);        System.out.println(m1.toGenericString());        // 在具体对象执行方法        m1.invoke(test, "张三", "99");        // 如果第一个参数是null, 则该方法是静态方法        m1.invoke(null, "张三", "99");    }}class Test extends Thread {    private int id;    private String name;    private Test t;    private byte[] bs;    public String address;    public Test() {}    public Test(int id) {this.id = id;}    private Test(byte[] bs) {this.bs = bs;}    public Test(int id, String name, String address) {        this.id = id;        this.name = name;        this.address = address;    }    public Test(int id, String name, Test t)     {        this.id = id;        this.name = name;        this.t = t;    }    public void method1() {System.out.println("Test -> method1");}    public String method2() {return this.name;}    public String[] method3() {return null;}    public void method4() {System.out.println("Test -> method4" + id);}    public void method5(String name, String score) {System.out.println("Test -> method5; name=" + name + "; score=" + score);}}