- package cn.wbnull.reflect;
- import java.lang.reflect.Constructor;
- import java.lang.reflect.Method;
- public class Reflect {
- public static void main(String[] args) {
- try {
- Class<?> methodClass = Class.forName(”cn.wbnull.reflect.Reflect”);
- Constructor con = methodClass.getConstructor();
- Object obj = con.newInstance();
- Method method = methodClass.getMethod(”reflect”, String.class);
- String str = (String) method.invoke(obj, ”test”);
- System.out.println(”main:” + str);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public String reflect(String str) {
- String strReturn = ”reflect:” + str;
- System.out.println(strReturn);
- return strReturn;
- }
- }
package cn.wbnull.reflect;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
public class Reflect {
public static void main(String[] args) {
try {
Class<?> methodClass = Class.forName("cn.wbnull.reflect.Reflect");
Constructor con = methodClass.getConstructor();
Object obj = con.newInstance();
Method method = methodClass.getMethod("reflect", String.class);
String str = (String) method.invoke(obj, "test");
System.out.println("main:" + str);
} catch (Exception e) {
e.printStackTrace();
}
}
public String reflect(String str) {
String strReturn = "reflect:" + str;
System.out.println(strReturn);
return strReturn;
}
}
输出:
- reflect:test
- main:reflect:test
reflect:test
main:reflect:test