Java运行时具有以下内置类加载器: (1)Bootstrap class loader:它是虚拟机的内置类加载器,通常表示为null,并且没有父null; (2)Platform class loader:平台类加载器可以看到所有平台类,平台类中包括由平台类加载器或其祖先定义的Java SE平台API,其实现类的JDK特定的运行时类; (3)System class loader:它被称为应用程序类加载器,与平台类加载器不同。系统类加载器通常用于定义应用程序类路径,模块路径和JDK特定工具上的类。 (4)类加载器的继承关系:System的父类加载器为Platform,而Platform的父类加载器为Bootstrap。
publicstaticvoidmain(String[] args)throwsClassNotFoundException,NoSuchMethodException,IllegalAccessException,InvocationTargetException,InstantiationException{//获取字节码文件Class<?> c =Class.forName("itheima2.Student");Constructor<?>[] cons = c.getConstructors();for(Constructor con : cons){System.out.println(con);//输出了两个构造方法//public itheima2.Student(java.lang.String,int,java.lang.String)//public itheima2.Student()}System.out.println();Constructor<?>[] cons2 = c.getDeclaredConstructors();for(Constructor con : cons2){System.out.println(con);//输出所有构造方法//public itheima2.Student(java.lang.String,int,java.lang.String)//itheima2.Student(java.lang.String,int)//private itheima2.Student(java.lang.String)//public itheima2.Student()}System.out.println();//public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes):返回一个Constructor对象,该对象反映由此Class对象表示的类或接口的指定构造函数//public Constructor<?> getConstructor(Class<?>... parameterTypes):返回一个Constructor对象,该对象反映由此Class对象表示的类或接口的指定构造函数//以上两个方法的参数:要获得的构造方法的个数和数据类型对应的字节码文件对象//获取无参构造方法Constructor<?> con = c.getConstructor();//Constructor类提供了一个类的单个构造函数的信息和访问权限//public T newInstance(Object ... initargs):使用由此Constructor对象表示的构造函数,使用指定的初始化参数来创建和初始化构造函数的声明类的新实例。Object obj = con.newInstance();System.out.println(obj);//Student{name='null', age=0, address='null'}}
2.4 反射获取构造方法并使用练习1
练习1:通过反射实现如下操作: (1)Student s = new Student(“林青霞”,30,“西安”); (2)System.out.prinln(s); (3)基本数据类型也可以通过.class得到对应的Class类型
publicstaticvoidmain(String[] args)throwsClassNotFoundException,NoSuchMethodException,IllegalAccessException,InvocationTargetException,InstantiationException{//1、首先得到Class对象cClass<?> c =Class.forName("itheima2.Student");//2、通过c得到构造方法对象//基本数据类型也可以通过.class得到对应的class类型Constructor<?> con = c.getConstructor(String.class,int.class,String.class);//3、通过构造方法对象调用newInstance()生成一个对象Object obj = con.newInstance("林青霞",30,"西安");System.out.println(obj);}
练习2:通过反射实现如下操作: (1)Student s = new Student(“林青霞”); (2)System.out.println(s); (3)public void setAccessible(boolean flag):值为true,取消访问检查。
publicstaticvoidmain(String[] args)throwsClassNotFoundException,NoSuchMethodException,IllegalAccessException,InvocationTargetException,InstantiationException{Class<?> c =Class.forName("itheima2.Student");//因为要调用的为private修饰的构造方法,所以要用getDeclaredConstructor()Constructor<?> con = c.getDeclaredConstructor(String.class);//暴力反射//public void setAccessible(boolean flag):值为true,取消访问检查
con.setAccessible(true);//IllegalAccessException:不合法访问异常,因为我们虽然获取到了private修饰的方法,但我们不可以直接用私有的方法创建对象。而通过反射,我们可以通过暴力反射来调用私有方法。Object obj = con.newInstance("林青霞");System.out.println(obj);}
publicstaticvoidmain(String[] args)throwsClassNotFoundException,NoSuchMethodException,IllegalAccessException,InvocationTargetException,InstantiationException{Class<?> c =Class.forName("itheima2.Student");//Method[] methods = c.getMethods();Method[] methods = c.getDeclaredMethods();for(Method method : methods){System.out.println(method);}System.out.println();Method m = c.getDeclaredMethod("method");
m.setAccessible(true);//获取无参构造方法创建对象Constructor<?> con = c.getConstructor();Object obj = con.newInstance();
m.invoke(obj);}
2.7 反射获取成员方法并使用练习
练习:通过反射实现如下操作 (1)Student s = new Student(); (2)s.method1(); (3)s.method2(“林青霞”); (4)String ss = s.method3(“林青霞”,30); (5)System.out.println(ss); (6)s.function();