java 反射操作 实例分析(自己写哦)

新建一个类:

public class HPDJI {

	public static int cc=0;
	public int dd=0;
	
	public HPDJI(Integer dd) {
		super();
		this.dd = dd;
	}

	public static int getCc() {
		return cc;
	}

	public static void setCc(int cc) {
		HPDJI.cc = cc;
	}

	public int getDd() {
		return dd;
	}

	public void setDd(int dd) {
		this.dd = dd;
	}

	public int add(Integer k){
		dd=++dd+k;
		return dd;
	}
	public static int staticadd(Integer k){
		cc=++cc+k;
		return cc;
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Reflection re=new Reflection();
		HPDJI hd=new HPDJI(1);
		int d,c;
		try {
			Object[] arg={5};
			re.invokeMethod(hd, "add", arg);
			d=(int) re.getProperty(hd, "dd");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			Object[] arg={10};
			re.invokeStaticMethod("dji.com.luis.HPDJI", "staticadd", arg);
			c=(int) re.getStaticProperty("dji.com.luis.HPDJI","cc");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		HPDJI hd1 = null;
		Object[] obj={3};
		try {
			hd1=(HPDJI) re.newInstance("dji.com.luis.HPDJI", obj);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			re.isInstance(hd1, Class.forName("dji.com.luis.HPDJI"));
			re.isInstance(hd1, String.class);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int[] num={1,2,3,4,5,6,7,8,9};
		int x=(int) re.getByArray(num, 5);
		
	}

}


反射类:

import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;     
    
    
/**   
 * Java Reflection Cookbook   
 *   
 * @author Michael Lee   
 * @since 2006-8-23   
 * @version 0.1a   
 */    
    
public class Reflection {     
    /**   
     * 得到某个对象的公共属性   
     *   
     * @param owner, fieldName   
     * @return 该属性对象   
     * @throws Exception   
     *   
     */    
    public Object getProperty(Object owner, String fieldName) throws Exception {     
        Class ownerClass = owner.getClass();     
    
        Field field = ownerClass.getField(fieldName);     
    
        Object property = field.get(owner);     
    
        return property;     
    }     
    
    /**   
     * 得到某类的静态公共属性   
     *   
     * @param className   类名   
     * @param fieldName   属性名   
     * @return 该属性对象   
     * @throws Exception   
     */    
    public Object getStaticProperty(String className, String fieldName)     
            throws Exception {     
        Class ownerClass = Class.forName(className);     
    
        Field field = ownerClass.getField(fieldName);     
    
        Object property = field.get(ownerClass);     
    
        return property;     
    }     
    
    
    /**   
     * 执行某对象方法   
     *   
     * @param owner   
     *            对象   
     * @param methodName   
     *            方法名   
     * @param args   
     *            参数   
     * @return 方法返回值   
     * @throws Exception   
     */    
    public Object invokeMethod(Object owner, String methodName, Object[] args)     
            throws Exception {     
    
        Class ownerClass = owner.getClass();     
    
        Class[] argsClass = new Class[args.length];     
    
        for (int i = 0, j = args.length; i < j; i++) {     
            argsClass[i] = args[i].getClass();     
        }     
    
        Method method = ownerClass.getMethod(methodName, argsClass);     
    
        return method.invoke(owner, args);     
    }     
    
    
      /**   
     * 执行某类的静态方法   
     *   
     * @param className   
     *            类名   
     * @param methodName   
     *            方法名   
     * @param args   
     *            参数数组   
     * @return 执行方法返回的结果   
     * @throws Exception   
     */    
    public Object invokeStaticMethod(String className, String methodName,     
            Object[] args) throws Exception {     
        Class ownerClass = Class.forName(className);     
    
        Class[] argsClass = new Class[args.length];     
    
        for (int i = 0, j = args.length; i < j; i++) {     
            argsClass[i] = args[i].getClass();     
        }     
    
        Method method = ownerClass.getMethod(methodName, argsClass);     
    
        return method.invoke(null, args);     
    }     
    
    
    
    /**   
     * 新建实例   
     *   
     * @param className   
     *            类名   
     * @param args   
     *            构造函数的参数   
     * @return 新建的实例   
     * @throws Exception   
     */    
    public Object newInstance(String className, Object[] args) throws Exception {     
        Class newoneClass = Class.forName(className);     
    
        Class[] argsClass = new Class[args.length];     
    
        for (int i = 0, j = args.length; i < j; i++) {     
            argsClass[i] = args[i].getClass();     
        }     
    
        Constructor cons = newoneClass.getConstructor(argsClass);     
    
        return cons.newInstance(args);     
    
    }     
    
    
         
    /**   
     * 是不是某个类的实例   
     * @param obj 实例   
     * @param cls 类   
     * @return 如果 obj 是此类的实例,则返回 true   
     */    
    public boolean isInstance(Object obj, Class cls) {     
        return cls.isInstance(obj);     
    }     
         
    /**   
     * 得到数组中的某个元素   
     * @param array 数组   
     * @param index 索引   
     * @return 返回指定数组对象中索引组件的值   
     */    
    public Object getByArray(Object array, int index) {     
        return Array.get(array,index);     
    }     
}    


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值