详情
public class T{
public void operation(int i){}
public void operation(boolean m){}
}
如何通过反射调用operation方法呢,无论传进来的值是什么
Class typ = class.forName("T");
object obj = 5;
Method me = typ.getMethod("operation", new Class[]{obj.getClass()});
最后一行获得不到方法,由于基本类型int和Integer不同
应该怎么才能,不然反射多台就实现不了
java反射会用的请进,如何获得一个基本类型的类型。如int i=9;double j=5;通过这个数值,取得类型
答案:1 悬赏:0
解决时间 2021-02-22 03:06
- 提问者网友:相思瘸子
- 2021-02-21 07:34
最佳答案
- 二级知识专家网友:最后战士
- 2021-02-21 08:19
你可以这样试试:
Class typ = Class.forName("T");
Method me = null;
try {
me = typ.getMethod("operation", new Class[]{Integer.TYPE});
} catch (NoSuchMethodException ex) {
try {
me = typ.getMethod("operation", new Class[]{Integer.class});
} catch (NoSuchMethodException ex1) {
ex1.printStackTrace();
}
}
System.out.print(me);
Class typ = Class.forName("T");
Method me = null;
try {
me = typ.getMethod("operation", new Class[]{Integer.TYPE});
} catch (NoSuchMethodException ex) {
try {
me = typ.getMethod("operation", new Class[]{Integer.class});
} catch (NoSuchMethodException ex1) {
ex1.printStackTrace();
}
}
System.out.print(me);
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯