public class Class1 { public Class1() { super(); } public void test() throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException { Object cls = Class.forName("com.mgear.utils.test2").newInstance(); cls.getClass().getMethod("setName", new Class[]{String.class}).invoke(cls, new Object[]{"lyy"}); System.out.println(cls.getClass().getMethod("getName").invoke(cls)); } public static void main(String[] args) { Class1 c=new Class1(); try { c.test(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } }}
类映射小例
最新推荐文章于 2024-09-11 16:01:24 发布