/** * 获取一个Class类有三种方法: * @author Morgan */ public class GetClass { public static void main(String[] args) { //第一种获取方法,直接了当 Class<String> class1 = String.class; System.out.println(class1); //第二种获取方法,对实体类运用方法 String str = "just do it"; Class<?> class2 = str.getClass(); System.out.println(class2); //第三种获取方法,直接传递一个类名,字符串形式 try { Class<?> class3 = Class.forName("java.lang.String"); System.out.println(class3); } catch (ClassNotFoundException e) { e.printStackTrace(); } } }
/** * 获取一个Filed字段类的,有四种方法 * @author Morgan * */ public class GetFiled { public static void main(String[] args) { Class<String> class1 = String.class; //1.获取所有申明的字段 Field[] fields1 = class1.getDeclaredFields(); for (Field field : fields1) { System.out.println(field); } System.out.println("------------------------------------------------"); //2.获取所有的公共类字段 Field[] fields2 = class1.getFields(); for (Field field : fields2) { System.out.println(field); } System.out.println("------------------------------------------------"); //3.获取指定字段名(所有申明的字段都可获取) try { Field field1 = class1.getDeclaredField("value"); System.out.println(field1); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } System.out.println("------------------------------------------------"); //4.获取指定公共字段名 try { Field field2 = class1.getField("CASE_INSENSITIVE_ORDER"); System.out.println(field2); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } } }
/** * Method类:同Field有四种方法 * @author Morgan */ public class TestMethod { public static void main(String[] args) { Class<String> class1 = String.class; Method[] methods1 = class1.getDeclaredMethods(); for (Method method : methods1) { System.out.println(method); } System.out.println("------------------------------------------------"); Method[] methods2 = class1.getMethods(); for (Method method : methods2) { System.out.println(method); } System.out.println("------------------------------------------------"); try { Method method1 = class1.getDeclaredMethod("checkBounds",new Class[]{byte[].class,int.class,int.class}); System.out.println(method1); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } System.out.println("------------------------------------------------"); //如果方法中的无参数,则得到方法时,只需传一个方法名即可 try { //Method method2 = class1.getDeclaredMethod("hashCode"); //getMethod只获取public的方法 Method method2 = class1.getMethod("hashCode"); System.out.println(method2); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } } }
/** * Constructor类:同上有四种方法 * @author Morgan * */ public class TestConstructor { public static void main(String[] args) { Class<String> class1 = String.class; Constructor<?>[] cons1 = class1.getDeclaredConstructors(); for (Constructor<?> constructor : cons1) { System.out.println(constructor); } System.out.println("------------------------------------------------"); Constructor<?>[] cons2 = class1.getConstructors(); for (Constructor<?> constructor : cons2) { System.out.println(constructor); } System.out.println("------------------------------------------------"); try { Constructor<String> con1 = class1.getConstructor(char[].class); System.out.println(con1); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } System.out.println("------------------------------------------------"); try { Constructor<String> con2 = class1.getDeclaredConstructor(new Class[]{int.class,int.class,char[].class}); System.out.println(con2); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } } }
/** * 由一个class得到一个类的实体 * @author Morgan * */ public class TestGetInstance { public static void main(String[] args) { Class<String> class1 = String.class; try { //用此种方法,类必须有默认构造函数 String obj = (String) class1.newInstance(); System.out.println(obj.length()); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } Constructor<String> con1 = null; try { con1 = class1.getConstructor(char[].class); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } try { String str2 = (String) con1.newInstance(new char[]{'t','e','s','t'}); System.out.println(str2); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } }
/** * method方法执行 * @author Morgan * */ public class TestMethodInvoke { public static void main(String[] args) { String testStr = "testStr"; Method method = null; try { method = String.class.getMethod("charAt", new Class[]{int.class}); } catch (SecurityException e1) { e1.printStackTrace(); } catch (NoSuchMethodException e1) { e1.printStackTrace(); } try { Object ch = method.invoke(testStr, 1); System.out.println(ch); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } }