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方法