Java如何通过原始类型作为参数的反射来调用方法 [英] Java how to call method by reflection with primitive types as arguments
本文介绍了Java如何通过原始类型作为参数的反射来调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在类中有以下两种方法:
I have the following two methods in a class:
public void Test(int i){
System.out.println("1");
}
public void Test(Integer i){
System.out.println("2");
}
以下代码行
this.getClass().getMethod("Test",Integer.class).invoke(this, 10);
打印 2
,如何打印 1
?
推荐答案
使用基本类型作为参数调用方法反思:
To call a method with primitive types as parameters using reflection :
您可以使用
int.class
this.getClass().getMethod("Test",int.class).invoke(this, 10);
或 Integer.TYPE
this.getClass().getMethod("Test",Integer.TYPE).invoke(this, 10);
同样适用于其他原始类型
same applies for other primitive types
这篇关于Java如何通过原始类型作为参数的反射来调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文