现在的位置: 首页 > 综合 > 正文

黑马程序员–07类加载器

2018年05月11日 ⁄ 综合 ⁄ 共 1233字 ⁄ 字号 评论关闭

------- android培训java培训、期待与您交流! ----------

 类加载器简单的说就是加载类的工具,只要你用到某个类,就要将.class文件从硬盘加载进来,然后进行一些处理。

1、JAVA虚拟机中可以安装多个类加载器,系统默认三个主要类加载器,每个类负责加载特定位置的类:

BootStrap。ExtClassLoader,AppClassLoader

2、类加载器也是java类,因为其他是java类的类加载器本身也要被类加载器加载,显然必须有第一个类加载器不是java类,

这就是BootStrap。

3、java虚拟机中的所有类加载器采用具有父子关系的树形结构进行组织,在实例化每个类加载器对象时,需要为其指定一个

父级类加载器对象或者默认采用系统类加载器为父级类加载

public class ClassLoaderTest {
public static void main(String[] args) {
  System.out.println(ClassLoaderTest.class.getClassLoader().getClass().getName());
  System.out.println(System.class.getClassLoader()); //System是有BootStrap加载器加载的
  ClassLoader load = ClassLoaderTest.class.getClassLoader();
  while(load != null){
   System.out.println(load.getClass().getName());
   load = load.getParent();
  }
  System.out.println(load);
}
}

我们也可以指定自己的类加载器

 

用类加载方式管理资源配置文件,资源配置文件在以后的项目中是很常见的,如:项目中的多语言功能,一些参数的存储等等。

下面是对配置文件的读取方式,

1、InputStream ips=Test1.class.getClassLoader().getResourceAsStream("com/test3/config.properties");//绝对路径

2、InputStream ips=Test1.class.getResourceAsStream("config.properties");//相对路径
  Properties props=new Properties();
  props.load(ips);
  ips.close();
  String className=props.getProperty("admin");
  System.out.println("admin="+className);

------- android培训java培训、期待与您交流! ----------  详细请查看:http://edu.csdn.net/heima/

抱歉!评论已关闭.