反射实例测试代码
Posted kakaisgood
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了反射实例测试代码相关的知识,希望对你有一定的参考价值。
public class ReflectTest { public static void main(String[] args) { try { Class obj = ReflectTest.class.getClassLoader().loadClass("com.test.GetPackageName"); GetPackageName name = (GetPackageName) obj.getConstructor(String.class,String.class).newInstance("hello","xx"); System.out.println(name.toString()); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
public GetPackageName() { System.out.println(" 构造函数"); } public GetPackageName(String str) { System.out.println(str); } public GetPackageName(String str,String str2) { System.out.println("c2"+str+str2); }
以上是关于反射实例测试代码的主要内容,如果未能解决你的问题,请参考以下文章