欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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);
 }
}