JAVA反射了解

首先上两个文件已经打印结果

package ref.lect;

import java.util.Date;

public class Exe1 {
    
    public void publicMethod(String str){
        System.out.println("调用了::共有的方法,参数为:: " + str);
    }
    
    protected void protectedMethod(String str){
        System.out.println("调用了::受保护的方法,参数为:: " + str);
    }
    
    void xMethod(String str){
        System.out.println("调用了::受保护的方法,参数为:: " + str);
    }
    
    private int privateMethod(int num){
        System.out.println("调用了::受保护的方法,参数为:: " + num);
        return num;
    }
    
    private int id;
    public String name;
    protected Date birthday;
    
    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    
}
package ref.lect.invok;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;

import ref.lect.Exe1;

public class Envok1 {
    public static void main(String[] args) {
        try {
            System.out.println("=================获取类对象===============");
            Class<?> exe1 = Class.forName("ref.lect.Exe1");
            System.out.println("类名 :: "+exe1.getName());
            
            System.out.println("=================获取方法名===============");
            Method[] ms = exe1.getMethods();
            for (int i = 0; i < ms.length; i++) {
                System.out.println("方法名"+i+":: "+ms[i]);
            }
            
            System.out.println("=================获取成员名===============");
            Field[] fs = exe1.getDeclaredFields();
            for (int i = 0; i < fs.length; i++) {
                System.out.println("成员名"+i+":: "+fs[i]);
            }
            
            System.out.println("================= 设置列   private protected public ===============");
            Object obj = Class.forName("ref.lect.Exe1").getConstructor().newInstance();
            Field fn = Class.forName("ref.lect.Exe1").getField("name"); // public
            fn.set(obj, "刘德华");
            Exe1 e1 = (Exe1)obj;
            System.out.println("1:: 被set后的name值:: "+ e1.getName());
            System.out.println("1:: 被set后的birthday值:: "+ e1.getBirthday());
            System.out.println("1:: 被set后的id值:: "+ e1.getId());
            
            Field fb = Class.forName("ref.lect.Exe1").getDeclaredField("birthday");  // protected
            fb.setAccessible(true); // 暴力反射
            fb.set(obj, new Date());
            Exe1 e2 = (Exe1)obj;
            System.out.println("2:: 被set后的name值:: "+ e2.getName());
            System.out.println("2:: 被set后的birthday值:: "+ e2.getBirthday());
            System.out.println("2:: 被set后的id值:: "+ e2.getId());
            
            Field fi = Class.forName("ref.lect.Exe1").getDeclaredField("id");  // private
            fi.setAccessible(true); // 暴力反射
            fi.set(obj, 10086);
            Exe1 e3 = (Exe1)obj;
            System.out.println("3:: 被set后的name值:: "+ e3.getName());
            System.out.println("3:: 被set后的birthday值:: "+ e3.getBirthday());
            System.out.println("3:: 被set后的id值:: "+ e3.getId());
            
            System.out.println("=======================调用method======================");
            Object mPublic = Class.forName("ref.lect.Exe1").getConstructor().newInstance();
            Method m1 = Class.forName("ref.lect.Exe1").getMethod("publicMethod",String.class);
            System.out.println(m1);
            Exe1 exe4 = (Exe1)mPublic;
            m1.invoke(exe4, "张三");
            
            Object mPrivate = Class.forName("ref.lect.Exe1").getConstructor().newInstance();
            Method m2 = Class.forName("ref.lect.Exe1").getDeclaredMethod("privateMethod",int.class);
            System.out.println(m2);
            m2.setAccessible(true);
            Exe1 exe5 = (Exe1)mPrivate;
            int num = (Integer) m2.invoke(exe5, 666);
            System.out.println("返回结果 ==> num ==> :: " + num );
            
            
            
            
            
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
    }
}
执行结果:

=================获取类对象=============== 类名 :: ref.lect.Exe1 =================获取方法名=============== 方法名0:: public java.lang.String ref.lect.Exe1.getName() 方法名1:: public int ref.lect.Exe1.getId() 方法名2:: public void ref.lect.Exe1.setName(java.lang.String) 方法名3:: public java.util.Date ref.lect.Exe1.getBirthday() 方法名4:: public void ref.lect.Exe1.publicMethod(java.lang.String) 方法名5:: public void ref.lect.Exe1.setId(int) 方法名6:: public void ref.lect.Exe1.setBirthday(java.util.Date) 方法名7:: public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException 方法名8:: public final void java.lang.Object.wait() throws java.lang.InterruptedException 方法名9:: public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException 方法名10:: public boolean java.lang.Object.equals(java.lang.Object) 方法名11:: public java.lang.String java.lang.Object.toString() 方法名12:: public native int java.lang.Object.hashCode() 方法名13:: public final native java.lang.Class java.lang.Object.getClass() 方法名14:: public final native void java.lang.Object.notify() 方法名15:: public final native void java.lang.Object.notifyAll() =================获取成员名=============== 成员名0:: private int ref.lect.Exe1.id 成员名1:: public java.lang.String ref.lect.Exe1.name 成员名2:: protected java.util.Date ref.lect.Exe1.birthday ================= 设置列 private protected public =============== 1:: 被set后的name值:: 刘德华 1:: 被set后的birthday值:: null 1:: 被set后的id值:: 0 2:: 被set后的name值:: 刘德华 2:: 被set后的birthday值:: Tue May 08 16:35:33 CST 2018 2:: 被set后的id值:: 0 3:: 被set后的name值:: 刘德华 3:: 被set后的birthday值:: Tue May 08 16:35:33 CST 2018 3:: 被set后的id值:: 10086 =======================调用method====================== public void ref.lect.Exe1.publicMethod(java.lang.String) 调用了::共有的方法,参数为:: 张三 private int ref.lect.Exe1.privateMethod(int) 调用了::受保护的方法,参数为:: 666 返回结果 ==> num ==> :: 666

猜你喜欢

转载自www.cnblogs.com/shenbo-/p/9009180.html