Class类的getClassLoader()方法
阅读原文时间:2021年04月20日阅读:1
package minglu;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class HelloWorld {

    public static void main(String[] args) throws InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException {
        System.out.println("这是我的第一个进程");
        Class<?> cls;
        try {
            cls = Class.forName("minglu.HelloWorld");
            ClassLoader cLoader = cls.getClassLoader();
            System.out.println(cLoader.getClass().getName());
            System.out.println(HelloWorld.class.getClassLoader().getClass().getName());
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



    }

}

控制台输出结果:

这是我的第一个进程
sun.misc.Launcher$AppClassLoader
sun.misc.Launcher$AppClassLoader


<p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px;"><span style="font-family: mceinline;">&nbsp; java是面向对象语言,面向对象的语言的宗旨就是万事万物皆对象,那么类也是一个对象,类里面的属性和方法也是对象。java里面的所</span></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px;"><span style="font-family: mceinline;">
</span></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px;"><span style="font-family: mceinline;">有的类都是Class类的对象,这个this.class是获得这个类相对于Class类的对象。后面的方法是获得这个类对象的加载器。</span></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px;"><span style="font-family: mceinline;">
</span></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px;"><span style="font-family: mceinline;"><span style="color:#ff0000;">只有Class类才有getClassLoader()方法呀</span>~ 可以这么想,我们平时讲述某某类,但是我们并没有说这个类怎么和虚拟机打交道,虚拟机怎么识别这</span></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px;"><span style="font-family: mceinline;">
</span></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px;"><span style="font-family: mceinline;">个类.总不能全靠字符串吧. 所以呢java就设计了Class这个类.用于虚拟机对类的管理.当一个类被虚拟机装载完毕的时候,就会创建一个Class类</span></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px;"><span style="font-family: mceinline;">
</span></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px;"><span style="font-family: mceinline;">的实例,对于类A就是A.class,对于类B就是B.class. Class类也提供了许多方法来获取类的信息. 要知道,类的装载器分为 "启动类装载器 ", "用</span></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px;"><span style="font-family: mceinline;">
</span></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; font-size: 14px; line-height: 26px;"><span style="font-family: mceinline;">户定义装载器 ".它不止一种 Class类需要保存这些信息. getClassLoader()是用来获取这个信息的</span></p>

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器