求教Java里面如何反射invoke父类的overriden方法
2个回答
展开全部
import java.lang.reflect.Method;
public class InvokeTest {
public static void main(String[] args) {
Class<?> clazz;
Object obj;
try {
InvokeTest it = new InvokeTest();
clazz = Class.forName("com.test.invoke.User");
obj = clazz.newInstance();
Param p = new Param();
p.setMethodName("introduction");
p.setParamTypes(new Class<?>[]{String.class,int.class});
p.setParamValuese(new Object[]{"Tom",20});
Object result = it.call(obj, p);
System.out.println(result+"");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 反射方法调用
* @param declareCLass classInstatnce
* @param param 参数设置类
* @return
* @throws Exception
*/
public Object call(Object declareCLass, Param param) throws Exception {
Class<?> clazz = declareCLass.getClass(); //获取class对象
String name = param.getMethodName(); //从param类中获取调用方法名
Class<?>[] methodParamTypes = param.getParamTypes(); //从param类中获取调用方法参数类型
Object[] objs = param.getParamValuese(); //从param类中获取调用方法参数值
Method method = clazz.getDeclaredMethod(name, methodParamTypes); //通过class对象获取对应的方法
return method.invoke(declareCLass, objs); //invoke调用方法,传入 classInstance、 参数值
}
}
/**
* 参数设置类
* @author Administrator
*
*/
class Param {
private String methodName;
private Class<?>[] paramTypes;
private Object[] paramValuese;
public String getMethodName() {
return methodName;
}
public void setMethodName(String methodName) {
this.methodName = methodName;
}
public Class<?>[] getParamTypes() {
return paramTypes;
}
public void setParamTypes(Class<?>[] paramTypes) {
this.paramTypes = paramTypes;
}
public Object[] getParamValuese() {
return paramValuese;
}
public void setParamValuese(Object[] paramValuese) {
this.paramValuese = paramValuese;
}
}
class User {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
System.out.println("called method setName value = " + name);
}
public int getAge() {
return age;
}
public void setAge(int age) {
System.out.println("called method setAge value = " + age);
this.age = age;
}
public String introduction(String name,int age){
return "My name is "+name+". I'm "+age+" years old.";
}
}
public class InvokeTest {
public static void main(String[] args) {
Class<?> clazz;
Object obj;
try {
InvokeTest it = new InvokeTest();
clazz = Class.forName("com.test.invoke.User");
obj = clazz.newInstance();
Param p = new Param();
p.setMethodName("introduction");
p.setParamTypes(new Class<?>[]{String.class,int.class});
p.setParamValuese(new Object[]{"Tom",20});
Object result = it.call(obj, p);
System.out.println(result+"");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 反射方法调用
* @param declareCLass classInstatnce
* @param param 参数设置类
* @return
* @throws Exception
*/
public Object call(Object declareCLass, Param param) throws Exception {
Class<?> clazz = declareCLass.getClass(); //获取class对象
String name = param.getMethodName(); //从param类中获取调用方法名
Class<?>[] methodParamTypes = param.getParamTypes(); //从param类中获取调用方法参数类型
Object[] objs = param.getParamValuese(); //从param类中获取调用方法参数值
Method method = clazz.getDeclaredMethod(name, methodParamTypes); //通过class对象获取对应的方法
return method.invoke(declareCLass, objs); //invoke调用方法,传入 classInstance、 参数值
}
}
/**
* 参数设置类
* @author Administrator
*
*/
class Param {
private String methodName;
private Class<?>[] paramTypes;
private Object[] paramValuese;
public String getMethodName() {
return methodName;
}
public void setMethodName(String methodName) {
this.methodName = methodName;
}
public Class<?>[] getParamTypes() {
return paramTypes;
}
public void setParamTypes(Class<?>[] paramTypes) {
this.paramTypes = paramTypes;
}
public Object[] getParamValuese() {
return paramValuese;
}
public void setParamValuese(Object[] paramValuese) {
this.paramValuese = paramValuese;
}
}
class User {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
System.out.println("called method setName value = " + name);
}
public int getAge() {
return age;
}
public void setAge(int age) {
System.out.println("called method setAge value = " + age);
this.age = age;
}
public String introduction(String name,int age){
return "My name is "+name+". I'm "+age+" years old.";
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询