public static void main(String args[]) { try { Class<?> class1=Class.forName("java.lang.String"); Method[] methods=class1.getDeclaredMethods(); System.out.println("-------------------------------------------------------"); Method method=class1.getDeclaredMethod("length",new Class[]{}); Method method1=class1.getDeclaredMethod("substring",new Class[]{int.class}); Method method2=class1.getDeclaredMethod("hashCode",null); System.out.println(method); System.out.println(method1); System.out.println(method2); System.out.println("-------------------------------------------------------"); Field[] fields=class1.getDeclaredFields(); for(Field f:fields){ System.out.println(f); } System.out.println("-------------------------------------------------------"); Constructor constructor=class1.getDeclaredConstructor(new Class[]{String.class}); Constructor constructor1=class1.getDeclaredConstructor(new Class[]{}); byte[] bytes=new byte[1024]; System.out.println("----"+bytes.getClass()); Constructor constructor2=class1.getDeclaredConstructor(new Class[]{bytes.getClass(),int.class}); System.out.println(constructor); System.out.println(constructor1); System.out.println(constructor2); // Constructor[] constructors=class1.getDeclaredConstructors(); /* for( Constructor c:constructors){ System.out.println(c); }*/ } catch (Exception e) { e.printStackTrace(); } }
Java反射的简单练习
最新推荐文章于 2023-09-03 14:39:22 发布