gpt4 book ai didi

java - ImageIO.read(getClass().getResource ("imagepath")) 中 getclass() 的实际用途是什么

转载 作者:行者123 更新时间:2023-12-02 12:17:44 26 4
gpt4 key购买 nike

我正在浏览 ImageIO,并且在网络上阅读了这段代码,但无法弄清楚为什么在这种情况下需要 getClass() 。 ( getClass() 返回我所理解的类名 -> 如果我错了请纠正我)。那么,为什么我们需要 getClass() 呢?

最佳答案

.getResource()方法Class<?>对象(这是任何 .getClass() 调用都会返回的)最终调用 ClassLoader正在运行的 JVM;你需要一个 ClassLoader为了访问正在运行的类路径中的资源。

这记录在the relevant javadoc中:

Finds a resource with a given name. The rules for searching resources associated with a given class are implemented by the defining class loader of the class.

关于java - ImageIO.read(getClass().getResource ("imagepath")) 中 getclass() 的实际用途是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46038664/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com