首页 > 代码库 > java反射小练习
java反射小练习
package com.gmw; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; public class ReflectTest { void test(String name,String word){ System.out.println(name+"说"+word); } public ReflectTest(String name) { } public ReflectTest() {} public static void main(String[] args) { try { Class clazz = Class.forName("com.gmw.ReflectTest"); Constructor<?> cons = clazz.getConstructor(String.class); ReflectTest test = (ReflectTest)cons.newInstance(""); test.test("babybear", "我们分手吧"); } catch (ClassNotFoundException 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(); } 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(); } } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。