• Java初学者笔记六:反射


    Java反射基础



    零、基础类代码

    import java.io.*;
    import java.lang.reflect.*;
    class father{
    	public String fName;
    	father(String name) {
    		this.fName = name;
    	}
    	public void show() throws Exception{
    		Runtime.getRuntime().exec("touch 2.txt");
    	}
    }
    
    class child extends father{
    	public int cAge;
    	child(int age,String name){
    		super(name);
    		this.cAge = age;
    	}
    	public void display() {
    		System.out.println("I am Child!");
    	}
    }
    

    一、根据对象和类获取类


    方法一 -> getClass()

    对运行时候的对象调用getClass获取其类对象

    public class InvokeLearn{
    	public static void main(String[] args) throws Exception{
    		father f = new father("TOM");
    		child c = new child(5,"JIM");
    		String clsname = c.getClass().getName();//可以获取类,然后getName返回类名字符串
        System.out.println(clsname);
    	}
    }
    

    方法二 -> class属性

    对类本身调用class属性

    public class InvokeLearn{
    	public static void main(String[] args) throws Exception{
    	    //Runtime.getRuntime().exec("touch 1.txt");
    		father f = new father("TOM");
    		child c = new child(5,"JIM");
    		String clsname = c.getClass().getName();
    		Class<child> clsname1 = child.class;
    		System.out.println(clsname1.getName());
    	}
    }
    

    方法三 -> Class.forName()

    使用Class.forName()方法

    public class InvokeLearn{
    	public static void main(String[] args) throws Exception{
    	    //Runtime.getRuntime().exec("touch 1.txt");
    		father f = new father("TOM");
    		child c = new child(5,"JIM");
    		Class cls = Class.forName("father");
    	}
    }
    

    二、根据类获取构造方法并创建实例


    • 常用的几个方法:
      • getDeclaredConstructor()
      • getDeclaredConstructors()
      • getConstructors()
      • getConstructor()
    public class InvokeLearn{
    	public static void main(String[] args) throws Exception{
        Class cls = Class.forName("father");
        Constructor[] conArray= cls.getDeclaredConstructors();//所有构造方法
        /*
        * getConstructors() ->所有公有的构造方法
        * getConstructors(null) -> 所有公有的无餐的构造方法
        * getDeclaredConstructor(parameters_type) -> 私有的含参的构造方法,parameters_types是参数的类型
        */
        //System.out.print(conArray.length);
        Constructor newc = conArray[0];
        Object obj = newc.newInstance("Tom");//调用构造方法创建实例
    	}
    }
    

    三、根据类获取成员变量并使用


    • 常用的几个方法:
      • getDeclaredField()
      • getDeclaredFileds()
      • getFields()
      • getField()
    public class InvokeLearn{
    	public static void main(String[] args) throws Exception{
    		Class cls = Class.forName("father");
    		Constructor[] conArray= cls.getDeclaredConstructors();//所有构造方法
    		Constructor newc = conArray[0];
    		Object obj = newc.newInstance("Tom");//创建实例
    		Field dis = cls.getDeclaredField("fName");//获取属性对象
    		dis.set(obj,"George");//设置属性值
    		System.out.println(dis.get(obj));//获取属性值并打印
    	}
    }
    

    四、根据类获取成员方法并使用


    • 常用的几个方法:
      • getDeclaredMethod()
      • getDeclaredMethods()
      • getMethods()
      • getMethod()
    public class InvokeLearn{
    	public static void main(String[] args) throws Exception{
    		Class cls = Class.forName("father");
    		Constructor[] conArray= cls.getDeclaredConstructors();//所有构造方法
    		Constructor newc = conArray[0];
    		Object obj = newc.newInstance("JJJ");//创建实例
    		Method dis = cls.getDeclaredMethod("show",null);//获取show方法
    		dis.invoke(obj, null);//调用show方法
    	}
    }
    
  • 相关阅读:
    wpf 用c#代码给img指定uri
    c 指针作为出参
    wpf获得系统毫秒数
    绑定元素的长宽(Canvas的子类自动伸展)
    PB与COM之关于创建COM,MTS, and COM+组件(1)
    ASA破解密码
    遭遇奸商(显卡篇)
    “启动Word时提示出错,只能用安全模式才能打开”的解决方法
    PowerSocket对象与HostName
    制做集成SATA驱动的XP安装盘
  • 原文地址:https://www.cnblogs.com/KevinGeorge/p/8663103.html
Copyright © 2020-2023  润新知