Java反射
反射的使用方法 userSeriver接口 public interface UserService { public class UserServiceImpl implements UserService { public class MainTest { ?? ?public static void main(String[] args) {?? ??? ?try {?? ??? ??? ?Class<?> clazz1 = Class.forName("com.test.UserServiceImpl");?? ??? ??? ??? ??? ??? ?Method method1 = clazz1.getMethod("update",String.class,String.class);?? ??? ??? ?Object invoke1 = method1.invoke(clazz1.newInstance(),"id100","王大拿","51");?? ??? ??? ?System.out.println(invoke1);?? ??? ??? ??? ??? ??? ?Class<?> clazz2 = UserServiceImpl.class;?? ??? ??? ?for (Method method : clazz2.getMethods()) {?? ??? ??? ??? ?if (method.getName().equals("insert")) {?? ??? ??? ??? ??? ?Method thisMethod = clazz2.getMethod(method.getName(),method.getParameterTypes());?? ??? ??? ??? ??? ?Object obj = thisMethod.invoke(clazz2.newInstance(),"1","张大炮","28");?? ??? ??? ??? ??? ?System.out.println(obj);?? ??? ??? ??? ?}?? ??? ??? ?}?? ??? ??? ??? ??? ??? ?UserService user = new UserServiceImpl();?? ??? ??? ?Class<?> clazz3 = user.getClass();?? ??? ??? ?Method method3 = clazz3.getMethod("add");?? ??? ??? ?Object invoke3 = method3.invoke(clazz3.newInstance());?? ??? ??? ?System.out.println(invoke3);?? ??? ??? ??? ??? ?} catch (Exception e) {?? ??? ??? ?e.printStackTrace(http://www.my516.com);?? ??? ?}?? ?}}?--------------------- (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |