JDK文档的解释!getResource public URL getResource(String name) Finds a resource with a given name. This method returns null if no resource with this name is found. The rules for searching resources associated with a given class are implemented by the * defining class loader of the class. This method delegates the call to its class loader, after making these changes to the resource name: if the resource name starts with "/", it is unchanged; otherwise, the package name is prepended to the resource name after converting "." to "/". If this object was loaded by the bootstrap loader, the call is delegated to ClassLoader.getSystemResource. Parameters: name - name of the desired resource Returns: a java.net.URL object. Since: JDK1.1 See Also: ClassLoader
JDK文档的解释! forName public static Class forName(String className) throws ClassNotFoundException Returns the Class object associated with the class or interface with the given string name. Invoking this method is equivalent to: Class.forName(className, true, currentLoader)
where currentLoader denotes the defining class loader of the current class. For example, the following code fragment returns the runtime Class descriptor for the class named java.lang.Thread: Class t = Class.forName("java.lang.Thread")
java.lang.Class.getResource(java.lang.String)
它是calss类的一个方法!
我的理解是: 得到 L 类的当前路径的一个文件1.gif的URL路径!请看实例:import java.net.*;
public class ClassTest
{
public static void main(String[] args) throws ClassNotFoundException
{
URL url=Class.forName("ClassTest").getResource("a.class");
System.out.println(url.toString());
System.out.println("Hello World!");
}
}
---------- 运行Java ----------
file:/D:/MyJava/others/a.class
Hello World!输出完成 (耗时 0 秒) - 正常终止a.class是以前做测试的一个文件,和ClassTest.class在同一个文件下!
不知是否正确,请多多讨论!
public URL getResource(String name)
Finds a resource with a given name. This method returns null if no resource with this name is found. The rules for searching resources associated with a given class are implemented by the * defining class loader of the class.
This method delegates the call to its class loader, after making these changes to the resource name: if the resource name starts with "/", it is unchanged; otherwise, the package name is prepended to the resource name after converting "." to "/". If this object was loaded by the bootstrap loader, the call is delegated to ClassLoader.getSystemResource.
Parameters:
name - name of the desired resource
Returns:
a java.net.URL object.
Since:
JDK1.1
See Also:
ClassLoader
forName
public static Class forName(String className)
throws ClassNotFoundException
Returns the Class object associated with the class or interface with the given string name. Invoking this method is equivalent to:
Class.forName(className, true, currentLoader)
where currentLoader denotes the defining class loader of the current class.
For example, the following code fragment returns the runtime Class descriptor for the class named java.lang.Thread: Class t = Class.forName("java.lang.Thread")