在 Java 中访问其他类文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4357250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 16:26:46  来源:igfitidea点击:

Accessing other class files in Java

javaclass

提问by Hyman Franklin

We've just started learning Java for my degree and I've been given a folder with various Java classes in, each in their own .java file, with the file name the same as the name of the class it has it in.

我们刚刚开始为我的学位学习 Java,我得到了一个文件夹,里面有各种 Java 类,每个类都在自己的 .java 文件中,文件名与它所在的类的名称相同。

There is one file which hosts a public class which has the following in:

有一个文件承载一个公共类,其中包含以下内容:

public static void main(String[] args) {}

This creates a new instance of another class which is stored in a separate .java file, and many of the classes (each in their own .java file) seem to reference other classes without having to put anything such as

这会创建另一个类的新实例,该实例存储在单独的 .java 文件中,并且许多类(每个类都在它们自己的 .java 文件中)似乎引用了其他类,而无需放置任何东西,例如

include("otherclass.php")

If you were working in PHP.

如果您使用 PHP。

My question is: Is this how Java does things? So you can happily refer to other classes and create new instances of a class from another .java file as long as they are in the same directory?

我的问题是:这是 Java 的工作方式吗?因此,只要它们在同一目录中,您就可以愉快地引用其他类并从另一个 .java 文件创建类的新实例吗?

I hope my question makes some sense!

我希望我的问题有意义!

Thanks,

谢谢,

Hyman.

Hyman。

采纳答案by BalusC

There the importstatement is for. The other class only has to be in the classpath. The classpath is basically a collection of file system paths pointing to the package root and/or individial JAR file(s). Using the importstatement is indeed not necessary when the class is in the same package as the current class (the same directory, as you say), or when you're referencing them by the full qualified name like

还有的import说法是。另一个类只需要在类路径中。类路径基本上是指向包根和/或单个 JAR 文件的文件系统路径的集合。import当类与当前类位于同一个包中(如您所说的同一目录)时,或者当您通过完全限定的名称引用它们时,确实没有必要使用该语句

com.example.OtherClass otherClass = new com.example.OtherClass();

Also, the classes of the java.langpackage is always implicitly imported, you don't need to explicitly import them nor explicitly specify their path in the classpath.

此外,java.lang包的类始终是隐式导入的,您不需要显式导入它们,也不需要在类路径中显式指定它们的路径。

See also:

也可以看看:

回答by DGH

BalusC is correct.

BalusC 是正确的。

Java also uses an OS level environment variable called "CLASSPATH" to determine what folder(s) to look in for other .class files you may be referencing in your program. It will load classes found in the CLASSPATH as they are used with no need for an import statement. CLASSPATH almost always includes the current folder you are running the program from.

Java 还使用名为“CLASSPATH”的操作系统级环境变量来确定要在哪些文件夹中查找您可能在程序中引用的其他 .class 文件。它将加载在 CLASSPATH 中找到的类,因为它们无需导入语句即可使用。CLASSPATH 几乎总是包含您运行程序的当前文件夹。

Java IDEs will usually have options for modifying CLASSPATH. You can also adjust it through whatever method your OS uses for setting environment variables.

Java IDE 通常有修改 CLASSPATH 的选项。您还可以通过您的操作系统用于设置环境变量的任何方法来调整它。

回答by Flinsy

To append what what the previous gentlemen we're saying.

补充一下我们之前的先生们所说的话。

In java, if you have a public class file i.e. a file containing one class defined as

在java中,如果你有一个公共类文件,即包含一个定义为的类的文件

public class Whatever{
    enter code here
}

Use can directly use it in other files by just saying

使用可以直接在其他文件中使用,只要说

Whatever we = new Whatever();