java反射(3)--反射的调用方法

1.通过发射调用类中的方法

1.调用没有返回值没有参数的方法

package org.sh.reflect2;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class InvokeDemo {
	public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, InstantiationException {
		Class<?> c = null;
		try {
			c = Class.forName("org.sh.reflect2.Person");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		Method met = null;
		try {
			met = c.getMethod("sayHello");
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}
		met.invoke(c.newInstance());
	}
}

1.调用有返回值没有参数的方法

package org.sh.reflect2;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class InvokeDemo02 {
	public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, InstantiationException {
		Class<?> c = null;
		try {
			c = Class.forName("org.sh.reflect2.Person");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		Method met = null;
		try {
			met = c.getMethod("getInfo");
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}
		String val = (String)met.invoke(c.newInstance());
		System.out.println(val);
	}
}

1.调用有返回值有参数的方法

package org.sh.reflect2;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class InvokeDemo03 {
	public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, InstantiationException {
		Class<?> c = null;
		try {
			c = Class.forName("org.sh.reflect2.Person");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		Method met = null;
		try {
			met = c.getMethod("say",String.class,String.class);
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}
		String val = (String)met.invoke(c.newInstance(),"suhao","欢迎你");
		System.out.println(val);
	}
}

2.通过发射调用类中的setter和getter方法

package org.sh.reflect2;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class InvokeDemo04 {
	public static void main(String[] args) throws Exception {
		Class<?> c = null;
		c = Class.forName("org.sh.reflect2.Person");
		Object o = c.newInstance();
		set("name","suhao",o,String.class);
		set("age",30,o,int.class);
		System.out.println(get("name",o));
		System.out.println(get("age",o));
	}
	
	/**
	 * 
	 * @param name 属性名称
	 * @param value 属性值
	 * @param obj 对象
	 */
	public static void set(String name,Object value,Object obj,Class<?> type) throws Exception{
		Method met = obj.getClass().getMethod("set"+initStr(name), type);
		met.invoke(obj, value);
	}
	
	public static Object get(String name,Object obj) throws Exception{
		Method met = obj.getClass().getMethod("get"+initStr(name));
		Object value =  met.invoke(obj);
		return value;
	}
	
	public static String initStr(String name){
		StringBuffer buf = new StringBuffer();
		buf.append(name.substring(0,1).toUpperCase()).append(name.substring(1));
		return buf.toString();
	}
}


3.通过发射直接操作类的属性

package org.sh.reflect2;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class InvokeDemo05 {
	public static void main(String[] args) throws Exception {
		Class<?> c = null;
		c = Class.forName("org.sh.reflect2.Person");
		Object obj = c.newInstance();
		Field nameField = c.getDeclaredField("name");
		nameField.setAccessible(true);//让属性可见
		nameField.set(obj, "suhao");
		System.out.println(nameField.get(obj));
		
	}
	
	
}

属性也可以直接操作,但不建议这么做 因为不安全 最好使用上面的setter和getter方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值