位置:java.lang.Class
类对象,类的实例代表一个运行 类 java应用程序的类和接口。
1 2 3 public final class Class <T> extends Object implements Serializable , GenericDeclaration, Type, AnnotatedElement
2.1 常用方法和示例 常用方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 String getName () 返回由 类对象表示的实体(类,接口,数组类,原始类型或空白)的名称,作为 String 。static Class<?> forName(String className) 获取类对象名 Package getPackage () 获取类对象的包名 Class<? super T> getSuperclass() 获取父类的类对象名 Class<?>[] getInterfaces() 获取接口的类对象名 Constructor<?>[] getConstructors() 获取构造方法 Class<?>[] getParameterTypes() 获取方法(构造/成员)的参数类型列表 Field[] getFields() 获取属性(自身+父类的所有public 公开属性) Field[] getDeclaredFields() 获取属性(自身所有的属性) Method[] getMethods() 获取方法(自身+父类单继承叠加的所有public 公开方法) Method[] getDeclaredMethods() 获取方法(自身所有的方法) T newInstance () 创建由此类对象表示的类的新实例(此类对象必须有无参构造)。
方法使用示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 public class TestClassMethods { public static void main (String[] args) throws Exception { Class<?> c = Class.forName("com.day.methods.Student" ); System.out.println(c.getName()); Package pack = c.getPackage(); System.out.println(pack.getName()); Class<?> superClass = c.getSuperclass(); System.out.println(superClass.getName()); Class<?>[] interfaces = c.getInterfaces(); for (Class<?> inter : interfaces) { System.out.print(inter.getName() + " " ); } Field[] fields = c.getFields(); for (Field field : fields) { System.out.print(field.getName() + " " ); } Field[] fields2 = c.getDeclaredFields(); for (Field field : fields2) { System.out.print(field.getName() + " " ); } System.out.println("\n---------------------" ); Method[] methods = c.getMethods(); for (Method method : methods) { System.out.print(method.getName() + " " ); } System.out.println("\n---------------------" ); Method[] methods2 = c.getDeclaredMethods(); for (Method method : methods2) { System.out.println(method.getName() + ":" + method.getReturnType()); } System.out.println("*************************" ); Constructor<?>[] cs = c.getConstructors(); for (Constructor<?> cc : cs) { System.out.print(cc.getName() + ":" ); Class<?>[] param = cc.getParameterTypes(); for (Class<?> p : param) { System.out.print(p.getName() + " " ); } System.out.println(); } System.out.println(); Object o = c.newInstance(); Student stu = (Student)o; System.out.println(stu); } }class Person { public String name; public double money; public Person () {} public void eat () {} public void sleep () {} }@SuppressWarnings({ "serial", "rawtypes" }) class Student extends Person implements Serializable , Runnable, Comparable{ public String name; public int age; double score; public Student () {super ();} public Student (String name) {} public Student (String name, int age) {} public void exam () {} public void play () {} @Override public void run () {} @Override public int compareTo (Object o) { return 0 ; } }
2.2 获取Class对象的 3 种方法 ① 通过类的对象,获取Class对象 ② 通过类名获取一个Class对象 ③ 通过Class的静态方法forName()获取类对象 【最具普适性】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 public class TestGetClassObject { public static void main (String[] args) throws ClassNotFoundException { Person p = new Person (); Class<? extends Person > c = p.getClass(); System.out.println(c.getName()); Class<Person> c2 = Person.class; System.out.println(c2.getName()); Class<?> c3 = Class.forName("com.day.reflect.Person" ); System.out.println(c3.getName()); } public static Class<?> getClassObject(String className) { Class<?> c = null ; try { c = Class.forName(className); } catch (ClassNotFoundException e) { e.printStackTrace(); } return c; } }class Person { }
2.3 反射创建对象的 2 种方法 ① 反射通过类对象创建类的对象 ② 方法传参返回对象 【普适性】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 public class TestNewInstance { public static void main (String[] args) throws Exception { Teacher t = new Teacher (); t.name = "JJ" ; System.out.println(t.name); Class<?> c = Class.forName("com.day.methods.Teacher" ); Teacher t2 = (Teacher)c.newInstance(); t2.name = "Jerry" ; System.out.println(t2.name); Teacher t3 = (Teacher)createObject("com.day.methods.Teacher" ); System.out.println(t3); } public static Object createObject (String className) { try { Class<?> c = Class.forName(className); return c.newInstance(); } catch (Exception e) { e.printStackTrace(); } return null ; } }class Teacher { String name; public Teacher () { } }