1. 程式人生 > >反射實例測試代碼

反射實例測試代碼

block acc rac str package ted trace new pack


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

反射實例測試代碼