求教Java里面如何反射invoke父类的overriden方法
答案:1 悬赏:30 手机版
解决时间 2021-03-13 11:30
- 提问者网友:听门外雪花风
- 2021-03-12 11:25
求教Java里面如何反射invoke父类的overriden方法
最佳答案
- 五星知识达人网友:woshuo
- 2021-03-12 12:28
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();
}
}
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、 参数值
}
}
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();
}
}
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、 参数值
}
}
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.";
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯