类加载器和反射

1.什么是类加载器

1.概念:类加载器是用来加载类的工具(从硬盘加载到JVM内存)

2.类加载的时机有哪些?
    一句话总结:类在使用时才被加载,不使用不加载。想想类什么时候被使用到呢?
    1)创建类的对象时
    2)通过类名调用静态方法时
    3)通过反射加载类
    
3.类加载器的分类(了解)
    启动类加载器:Bootstrap ClassLoaders,负责加载JAVA_HOME\lib目录下的jar和class文件
    平台类加载器:Platform ClassLoader
    应用程序类加载:System ClassLoader

2.类加载器应用

使用类加载器加载src目录下的配置文件s

Properties pro=new Properties();

//使用类加载器获取src目录下文件的输入流
InputStream in = ClassLoader.getssSystemClassLoader().getResourceAsStream("a.properties");
//把配置文件中的键和值,加载到Properties集合
pro.load(in);

System.out.prisntln(pro);

反射

反射可以在程序运行时,动态的获取每一个类的各个组成部分(成员变量Filed、成员方法Method、构造方法Constructor), 并使用。

 

1.获取clazz

//1. Class.forName("类的全路径名");
Class clazz1=Class.forName("com.ithiema.Student");

//2. 类名.class
Class clazz2=Student.class;

//3. 对象名.getClass()
Student stu=new Student();
Class clazz3=stu.getClass();

2.反射获取构造方法

public Constructor[] getConstructors()  
    获取构造方法的数组(public修饰的)
public Constructor getConstructor(Class<?>... parameterTypes)  
    获取一个构造方法(public修饰的)

public Constructor[] getDeclaredConstructors()  
    获取所有的构造方法(包括私有的)
public Constructor getDeclaredConstructor()
    获取一个构造方法(可以获取私有)

获取构造方法

//1.获取字节码
Class clazz = Class.forName("com.itheima.Student");

//2.获取多个构造方法
Constructor[] cons = clazz.getConstructors();
for (Constructor con : cons) {
    System.out.println(con);
}

System.out.println("---------------");
//3.获取空参数的构造方法
Constructor con = clazz.getConstructor();
System.out.println(con);

//4.获取有两个参数构造方法(私有的)
Constructor con2 = clazz.getDeclaredConstructor(String.class, int.class);
System.out.println(con2);

使用构造方法

//1.获取字节码
Class clazz = Class.forName("com.itheima.Student");

//2.获取空参数的构造方法
Constructor con = clazz.getConstructor();
//3.通过空参数构造方法创建对象
//等价于 Student stu1=new Student();
Student stu1 = (Student) con.newInstance();
System.out.println(stu1);


//4.获取有两个参数构造方法(私有的)
Constructor con2 = clazz.getDeclaredConstructor(String.class, int.class);
//通过有参数构造方法创建对象
//等价于 Student stu2=new Student("张三",20);
//如果构造方法是私有的,需要调用方法取消权限检查(暴力反射)
con2.setAccessible(true);
Student stu2  = (Student) con2.newInstance("张三", 20);
System.out.println(stu2);

//直接使用字节码创建对象(默认是按照空参数构造方法创建的对象)
Object o = clazz.newInstance();
System.out.println(o);

3.反射获取成员变量

public Field[] getFields()  
    获取多个成员变量(public修饰的)
public Field getField(String name)  
    获取某一个成员变量(public修饰的)
    
public Field[] getDeclaredFields() 
    获取所有的成员变量(包括私有的)
public Field getDeclaredField(String name)  
    获取一个成员变量(可以获取私有的)

获取成员变量

//1.获取字节码对象
Class clazz = Class.forName("com.itheima02.Student");

//2.获取所有公有成员变量
Field[] fields = clazz.getFields();
for (Field field : fields) {
    System.out.println(field);
}

//3.获取所有的成员变量(所有的都要)
Field[] fields1 = clazz.getDeclaredFields();
for (Field field : fields1) {
    System.out.println(field);
}

使用成员变量

//1.获取字节码对象
Class clazz = Class.forName("com.itheima02.Student");

//2.创建Student对象,反射创建对象
Student obj = (Student) clazz.newInstance();

//3.获取一个成员变量age
Field ageFiled = clazz.getField("age");
//set:给属性设置值
ageFiled.set(obj,18); //等价于 obj.setAge(18)
//get: 获取属性值
Integer ageValue = (Integer) ageFiled.get(obj); //等价于 int age= obj.getAge();
System.out.println(ageValue);

3.反射获取成员方法

public Method[] getMethods()  
    获取多个成员方法(public修饰的)
public Method getMethod(String name,Class ... parmars)  
    获取某一个成员方法(public修饰的)
    
public Method[] getDeclaredtMethods() 
    获取所有的成员方法(包括私有的)
public Method getDeclaredMethod(String name,Class ... parmars)  
    获取一个成员方法(可以获取私有的)

获取成员方法

//1.获取类的字节码
Class clazz = Class.forName("com.itheima04.Student");

//2.获取公有成员方法,父类方法也能获取到
Method[] ms = clazz.getMethods();
for (Method m : ms) {
    System.out.println(m);
}

//3.获取包括私有的成员方法,不包括父类的方法
Method[] ms1 = clazz.getDeclaredMethods();
for (Method method : ms1) {
    System.out.println(method);
}

//获取show()方法
Method m1 = clazz.getDeclaredMethod("show");
System.out.println(m1);

//获取show(String n)方法
Method m2 = clazz.getMethod("show", String.class);
System.out.println(m2);

//获取show(String n,int y)方法
Method m3 = clazz.getMethod("show", String.class, int.class);
System.out.println(m3);

//获取fun()方法
Method m4 = clazz.getMethod("fun");
System.out.println(m4);

 

反射+配置文件

  • 先写几个类

    public class Student {
        private String name;
        private int age;
    
        public void study(){
            System.out.println("学生在学习");
        }
    }
    
    public class Teacher {
        private String name;
        private int age;
    
        public void teach(){
            System.out.println("老师在讲课");
        }
    }

     写一个配置文件在src目录下

    classname=com.itheima05.Teacher
    methodname=teach

    写测试类

    //需求:通过配置文件+反射,在不改变源代码的情况下,可以执行任意一个类的任意方法。
    public class Demo5 {
        public static void main(String[] args) throws Exception {
            //读取配置文件
            Properties pro=new Properties();
            //使用类加载器读取src目录下文件
            InputStream in = ClassLoader.getSystemClassLoader().getResourceAsStream("config.properties");
            pro.load(in);
    
            //获取键对应的值
            //获取类名
            String classname = pro.getProperty("classname");
            //获取方法名
            String methodname = pro.getProperty("methodname");
    
            //通过反射获取classname对应的字节码
            Class<?> clazz = Class.forName(classname);
            //创建类的对象
            Object obj = clazz.getConstructor().newInstance();
    
            //通过反射获取methodname对应的方法
            Method method = clazz.getMethod(methodname);
            //执行方法
            method.invoke(obj);
        }
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值