java反射使用示例分享
程序员文章站
2024-02-22 17:37:22
复制代码 代码如下:public class reflextest { public static void main(string[]...
复制代码 代码如下:
public class reflextest {
public static void main(string[] args)
throws classnotfoundexception, nosuchmethodexception, securityexception,
illegalaccessexception, illegalargumentexception, invocationtargetexception,
instantiationexception {
//参数类型
class[] paramtypes = new class[3];
paramtypes[0] = string.class;
paramtypes[1] = integer.class;
paramtypes[2] = string.class;
//取得方法
method m = demo.class.getdeclaredmethod("getsome", paramtypes);
//参数设置
object[] os = new object[3];
os[0] = "pp";
os[1] = 4;
os[2] = "3";
//方法调用
m.invoke(demo.class.newinstance(), os);
}
}
class demo{
public void getsome(string name,integer year, string age){
system.out.println("name is :" + name + ";age is :" + age + ";i is :" + year);
}
}
上一篇: java反射使用示例分享
下一篇: .net 数据表格显示控件介绍