为什么getResourceAsStream方法在Class类中? [英] Why getResourceAsStream method is in Class class?

查看:106
本文介绍了为什么getResourceAsStream方法在Class类中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么公共InputStream getResourceAsStream(String name)在Class类中?它只是给出jar文件中文件的输入流,而与Class类没有关系。因此它可以是静态方法,并且可以在任何类中。

Why public InputStream getResourceAsStream(String name) is in Class class? It just give inputstream of file which is in jar file and there is no relation with Class class. so it can be static method and it can be in any class.

推荐答案

到该类:


  • 将考虑该类的包-如果您调用 getResourceAsStream( baz .txt)放在 foo.bar.SomeClass 的类上,它将查找 / foo / bar / baz。 txt

  • 首先考虑类加载器以查找资源-如果它是静态方法,它将如何知道哪些jar文件(例如)看?生活比系统类加载器更重要

  • The package of the class is taken into account - if you give call getResourceAsStream("baz.txt") on the class for foo.bar.SomeClass it will look for /foo/bar/baz.txt
  • The classloader is taken into account to find the resources in the first place - if it were a static method, how would it know which jar files (etc) to look in? There's more to life than the system classloader

这篇关于为什么getResourceAsStream方法在Class类中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆