「这是我参与11月更文挑战的第20天,活动详情查看:2021最后一次更文挑战」
类加载器:
类加载时机
类在什么时候会被加载到内存中呢?
- 创建类的实例(对象)
- 调用类的类方法
- 访问类或者接口的类变量,或者为该类变量赋值
- 使用反射方式来强制创建某个类或者接口对应的java.lang.Class对象
- 初始化某个类的子类
- 直接使用java.exe命令来运行某个主类。
用到就加载,不用不加载
类加载的过程
public class ClassLoaderDemo1 {
public static void main(String[] args) {
// 获取系统类加载器
ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
// 获取系统类加载器的父加载器 --- 平台类加载器
ClassLoader classLoader1 = systemClassLoader.getParent();
// 获取平台类加载器的父加载器 --- 启动类加载器
ClassLoader classLoader2 = classLoader1.getParent();
System.out.println("系统类加载器" + systemClassLoader);
System.out.println("平台类加载器" + classLoader1);
System.out.println("启动类加载器" + classLoader2);
// 系统类加载器jdk.internal.loader.ClassLoaders$AppClassLoader@3fee733d
// 平台类加载器jdk.internal.loader.ClassLoaders$PlatformClassLoader@3941a79c
// 启动类加载器null
}
}
public class ClassLoaderDemo2 {
public static void main(String[] args) throws IOException {
// 获取系统类加载器
ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
// 利用加载器去加载一个指定的文件
// 参数:文件的路径
// 返回值:字节流
InputStream is = systemClassLoader.getResourceAsStream("prop.properties");
Properties prop = new Properties();
prop.load(is);
System.out.println(prop);
is.close();
}
}
反射-概述
// 获取class对象的三种方式
public class ReflectDemo1 {
public static void main(String[] args) throws ClassNotFoundException {
//1. class 类中的静态方法 forName("全类名")
Class clazz = Class.forName("com.itheima.myreflect2.Student");
System.out.println(clazz);
//2. 通过class属性来获取
Class clazz2 = Student.class;
System.out.println(clazz2);
//3.利用对象的getClass方法来获取class对象
// getClass方法是定义在Object类中。
Student s = new Student();
Class clazz3 = s.getClass();
System.out.println(clazz3);
System.out.println(clazz == clazz2);
System.out.println(clazz2 == clazz3);
}
}
public class ReflectDemo3 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException {
//1. 获取Class对象
Class clazz = Class.forName("com.itheima.myreflect3.Student");
Constructor[] constructors = clazz.getConstructors();
for (Constructor constructor:
constructors) {
System.out.println(constructor);
}
// 返回所有构造方法对象的数组
Constructor[] constructors2 = clazz.getDeclaredConstructors();
for (Constructor constructor:
constructors2) {
System.out.println(constructor);
}
// 返回单个公共构造方法对象
// 小括号中,一定要跟构造方法的形参保持一致
Constructor constructors3 = clazz.getConstructor();
System.out.println(constructors3);
Constructor constructors4 = clazz.getConstructor(String.class,int.class);
System.out.println(constructors4);
// 因为student类中,没有只有一个int的构造,所以这里会报错。
Constructor constructors5 = clazz.getConstructor(int.class);
System.out.println(constructors5);
Constructor constructors6 = clazz.getDeclaredConstructor(String.class);
System.out.println(constructors6);
}
}
public class Student {
private String name;
private int age;
// 私有的有参构造方法
private Student(String name){
System.out.println("name的值为:"+name);
System.out.println("private...student...有参构造方法");
}
// 公共的无参构造方法
public Student(){
System.out.println("public...student...无参构造方法");
}
// 公共的有参构造方法
public Student(String name,int age){
System.out.println("name的值为:"+name+"age的值为:"+age);
System.out.println("public...student...有参构造方法");
}
}
public class ReflectDemo4 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
//1. 获取class 对象
Class clazz = Class.forName("com.itheima.myreflect3.Student");
//2. 获取构造方法对象
Constructor constructor = clazz.getConstructor(String.class, int.class);
//3. 利用newInstance 创建Student的对象
Student student = (Student) constructor.newInstance("zhangsan",23);
System.out.println(student);
// 获取构造方法对象
Constructor constructor2 = clazz.getConstructor();
Student student2 = (Student) constructor2.newInstance();
System.out.println(student2);
// 被private修饰的成员,不能直接使用
// 如果用反射强行获取并使用,需要临时取消访问检查
Constructor constructor3 = clazz.getDeclaredConstructor(String.class);
constructor3.setAccessible(true);
Student student3 = (Student)constructor3.newInstance("zhangsan");
System.out.println(student3);
}
}
public class Student {
public String name;
public int age;
public String gender;
private int money = 300;
}
public class Student {
//私有的,无参无返回值
private void show() {
System.out.println("私有的show方法,无参无返回值");
}
//公共的,无参无返回值
public void function1() {
System.out.println("function1方法,无参无返回值");
}
//公共的,有参无返回值
public void function2(String name) {
System.out.println("function2方法,有参无返回值,参数为" + name);
}
//公共的,无参有返回值
public String function3() {
System.out.println("function3方法,无参有返回值");
return "aaa";
}
//公共的,有参有返回值
public String function4(String name) {
System.out.println("function4方法,有参有返回值,参数为" + name);
return "aaa";
}
}
/**
* 获取Method对象
*/
public class ReflectDemo1 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException {
//method1();
//method2();
//method3();
//method4();
//method5();
}
private static void method5() throws ClassNotFoundException, NoSuchMethodException {
// Method getDeclaredMethod(String name, Class<?>... parameterTypes):
// 返回单个成员方法对象
//1.获取class对象
Class clazz = Class.forName("com.itheima.myreflect5.Student");
//2.获取一个成员方法show
Method method = clazz.getDeclaredMethod("show");
//3.打印一下
System.out.println(method);
}
private static void method4() throws ClassNotFoundException, NoSuchMethodException {
//1.获取class对象
Class clazz = Class.forName("com.itheima.myreflect5.Student");
//2.获取一个有形参的方法function2
Method method = clazz.getMethod("function2", String.class);
//3.打印一下
System.out.println(method);
}
private static void method3() throws ClassNotFoundException, NoSuchMethodException {
// Method getMethod(String name, Class<?>... parameterTypes) :
// 返回单个公共成员方法对象
//1.获取class对象
Class clazz = Class.forName("com.itheima.myreflect5.Student");
//2.获取成员方法function1
Method method1 = clazz.getMethod("function1");
//3.打印一下
System.out.println(method1);
}
private static void method2() throws ClassNotFoundException {
// Method[] getDeclaredMethods():
// 返回所有成员方法对象的数组,不包括继承的
//1.获取class对象
Class clazz = Class.forName("com.itheima.myreflect5.Student");
//2.获取Method对象
Method[] methods = clazz.getDeclaredMethods();
//3.遍历一下数组
for (Method method : methods) {
System.out.println(method);
}
}
private static void method1() throws ClassNotFoundException {
// Method[] getMethods():返回所有公共成员方法对象的数组,包括继承的
//1.获取class对象
Class clazz = Class.forName("com.itheima.myreflect5.Student");
//2.获取成员方法对象
Method[] methods = clazz.getMethods();
//3.遍历
for (Method method : methods) {
System.out.println(method);
}
}
}
/**
* 获取Method对象并运行
*/
public class ReflectDemo2 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InstantiationException, InvocationTargetException {
// Object invoke(Object obj, Object... args):运行方法
// 参数一:用obj对象调用该方法
// 参数二:调用方法的传递的参数(如果没有就不写)
// 返回值:方法的返回值(如果没有就不写)
//1.获取class对象
Class clazz = Class.forName("com.itheima.myreflect5.Student");
//2.获取里面的Method对象 function4
Method method = clazz.getMethod("function4", String.class);
//3.运行function4方法就可以了
//3.1创建一个Student对象,当做方法的调用者
Student student = (Student) clazz.newInstance();
//3.2运行方法
Object result = method.invoke(student, "zhangsan");
//4.打印一下返回值
System.out.println(result);
}
}