导入是如何在 Java 中完成的? [英] How is import done in Java?

查看:25
本文介绍了导入是如何在 Java 中完成的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如

import org.apache.nutch.plugin.Extension,

虽然多次使用,

我不太清楚本质上做了什么.

I've no much idea what is done essentially.

编辑:org.apache.nutch.plugin 本质上是 4 个目录还是少于 4 个,就像名为 org.apache 的目录一样?

EDIT: Is org.apache.nutch.plugin essentially 4 directories or fewer than 4 like a directory named org.apache?

推荐答案

我想你可能要问的问题是,Java 中的包是什么,import 关键字与他们有关吗?".您对目录结构的困惑可能源于这样一个事实,即某些其他语言具有 include 指令,这些指令在编译时使用文件名在源代码中逐字包含指定文件的内容.C/C++ 是使用这种类型的 include 指令的语言示例.Java 的 import 关键字不能以这种方式工作.正如其他人所说,import 关键字只是一种引用包中一个或多个类的简写方式.真正的工作是由 Java 虚拟机的类加载器完成的(详情如下).

I think the question you might be trying to ask is, "What are packages in Java, and how does the import keyword relate to them?". Your confusion about directory structures might stem from the fact that some other languages have include directives that use file names to literally include the contents of the specified file in your source code at compile time. C/C++ are examples of languages that use this type of include directive. Java's import keyword does not work this way. As others have said, the import keyword is simply a shorthand way to reference one or more classes in a package. The real work is done by the Java Virtual Machine's class loader (details below).

让我们从Java 包"的定义开始,如维基百科文章中所述:

Let's start with the definition of a "Java package", as described in the Wikipedia article:

Java 包是一种机制将 Java 类组织成类似于模块的命名空间模数.Java包可以存储在称为 JAR 文件的压缩文件,允许类下载得更快一组而不是一次一个.程序员通常还使用用于组织属于的类的包到同一类别或提供类似的功能.

A Java package is a mechanism for organizing Java classes into namespaces similar to the modules of Modula. Java packages can be stored in compressed files called JAR files, allowing classes to download faster as a group rather than one at a time. Programmers also typically use packages to organize classes belonging to the same category or providing similar functionality.

在 Java 中,类的源代码文件实际上是按目录组织的,但是 Java 虚拟机 (JVM) 定位类的方法与 C/C++ 等语言不同.

In Java, source code files for classes are in fact organized by directories, but the method by which the Java Virtual Machine (JVM) locates the classes is different from languages like C/C++.

假设在您的源代码中有一个名为com.foo.bar"的包,并且在该包中您有一个名为MyClass"的类.在编译时,该类的源代码在文件系统中的位置必须是 {source}/com/foo/bar/MyClass.java,其中 {source} 是您正在编译的源代码树的根.

Suppose in your source code you have a package named "com.foo.bar", and within that package you have a class named "MyClass". At compile time, the location of that class's source code in the file system must be {source}/com/foo/bar/MyClass.java, where {source} is the root of the source tree you are compiling.

Java 与 C/C++ 等语言之间的一个区别是类加载器的概念.事实上,类加载器的概念是 Java 虚拟机架构的关键部分.类加载器的工作是定位和加载您的程序需要的任何 class 文件.原始"或默认"Java 类加载器通常由 JVM 提供.它是 ClassLoader 类型的常规类,并包含一个名为 loadClass() 具有以下定义:

One difference between Java and languages like C/C++ is the concept of a class loader. In fact, the concept of a class loader is a key part of the Java Virtual Machine's architecture. The job of the class loader is to locate and load any class files your program needs. The "primordial" or "default" Java class loader is usually provided by the JVM. It is a regular class of type ClassLoader, and contains a method called loadClass() with the following definition:

// Loads the class with the specified name.
// Example: loadClass("org.apache.nutch.plugin.Extension")
Class loadClass(String name)

loadClass() 方法将尝试为具有给定名称的类定位 class 文件,并生成一个 Class 对象,它有一个 newInstance() 能够实例化类的方法.

This loadClass() method will attempt to locate the class file for the class with given name, and it produces a Class object which has a newInstance() method capable of instantiating the class.

类加载器在哪里搜索 class 文件? 在 JVM 的类路径中.类路径只是可以找到 class 文件的位置列表.这些位置可以是包含 class 文件的目录.它甚至可以包含 jar 文件,这些文件本身可以包含更多的 class 文件.默认的类加载器能够查看这些 jar 文件以搜索 class 文件.作为旁注,您可以实现自己的类加载器,例如,允许搜索网络位置(或任何其他位置)以查找 class 文件.

Where does the class loader search for the class file? In the JVM's class path. The class path is simply a list of locations where class files can be found. These locations can be directories containing class files. It can even contain jar files, which can themselves contain even more class files. The default class loader is capable of looking inside these jar files to search for class files. As a side note, you could implement your own class loader to, for example, allow network locations (or any other location) to be searched for class files.

所以,现在我们知道com.foo.bar.MyClass"是否在您自己的源代码树中的 class 文件或class 文件中jar 文件位于类路径中的某个位置,类加载器会为您找到它(如果存在).如果它不存在,你会得到一个 <代码>ClassNotFoundException.

So, now we know that whether or not "com.foo.bar.MyClass" is in a class file in your own source tree or a class file inside a jar file somewhere in your class path, the class loader will find it for you, if it exists. If it does not exist, you will get a ClassNotFoundException.

现在要解决 import 关键字:我将参考以下示例:

And now to address the import keyword: I will reference the following example:

import com.foo.bar.MyClass;

...

public void someFunction() {
    MyClass obj1 = new MyClass();
    org.blah.MyClass obj2 = new org.blah.MyClass("some string argument");
}

第一行只是告诉编译器的一种方式每当你看到一个变量被简单地声明为 MyClass 类型时,假设我的意思是 com.foo.bar.MyClass. 这就是 obj1 的情况.在 obj2 的情况下,你明确地告诉编译器我不想要 com 类.foo.bar.MyClass,我实际上想要 org.blah.MyClass".所以 import 关键字只是一种减少数量的简单方法为了使用其他类,打字程序员必须做的事情.所有有趣的事情都在 JVM 的类加载器中完成.

The first line is simply a way to tell the compiler "Whenever you see a variable declared simply as type MyClass, assume I mean com.foo.bar.MyClass. That is what's happening in the case of obj1. In the case of obj2, you are explicitly telling the compiler "I don't want the class com.foo.bar.MyClass, I actually want org.blah.MyClass". So the import keyword is just a simple way of cutting down on the amount of typing programmers have to do in order to use other classes. All of the interesting stuff is done in the JVM's class loader.

有关类加载器的确切功能的更多信息,我建议阅读一篇名为 Java 类加载器的基础

For more information about exactly what the class loader does, I recommend reading an article called The Basics of Java Class Loaders

这篇关于导入是如何在 Java 中完成的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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