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

查看:89
本文介绍了如何在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类组织到
名称空间中,类似于
Modula的模块。 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虚拟机架构的关键部分。类加载器的工作是找到并加载程序所需的任何文件。 原始或默认Java类加载器通常由JVM提供。它是 <$ c $类型的常规类c> 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 对象,其中包含 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.

类加载器在哪里搜索文件?在JVM的类路径中。类路径只是一个位置列表,其中可以找到 class 文件。这些位置可以是包含 class 文件的目录。它甚至可以包含 jar 文件,这些文件本身可以包含更多文件。默认的类加载器能够查看这些 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是否在你自己的文件中在您的类路径中的某个 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 keyword:我将参考以下示例:

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天全站免登陆