Java 引用同一目录中的类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21669272/
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-13 09:59:37  来源:igfitidea点击:

Java referencing a class in the same directory

javaclassimportpackage

提问by CodeKingPlusPlus

I created a Pairclass in Java (similar to the c++ pair) and am having trouble referencing it from a different java file. I am working inside a Java file, let's call it fileAin the same directory as Pair.class.Additionally, I have written package current-directoryat the top of both files.

Pair在 Java 中创建了一个类(类似于 C++ 对),但在从不同的 Java 文件中引用它时遇到了问题。我在一个 Java 文件中工作,让我们fileA在与Pair.class.另外一个目录相同的目录中调用它,我写package current-directory在两个文件的顶部。

However, when I try javac fileAall my errors are cannot find symboland the little arrow points to my custom Pairtype.

但是,当我尝试javac fileA所有错误时cannot find symbol,小箭头指向我的自定义Pair类型。

How do I get the java compiler to see Pair.classinside of fileA?

我如何让java编译器看到Pair.class里面fileA

Thanks for all the help

感谢所有的帮助

回答by Travis Wellman

Java is driven by some basic conventions, including that directory structure follows package structure, and java files are named after the classes they define.

Java 由一些基本约定驱动,包括目录结构遵循包结构,Java 文件以其定义的类命名。

You should have defined fileA as a class inside of fileA.java like so:

您应该将 fileA 定义为 fileA.java 中的一个类,如下所示:

public class fileA {
    public static void main(String[] args) {
        Pair p = new Pair(0, 1);
        System.out.println("a is "+p.a+" and b is "+p.b);
    }
}

and a corresponding Pair class:

和相应的 Pair 类:

public class Pair {
    public final int a;
    public final int b;
    public Pair(int a, int b) {
        this.a = a;
        this.b = b;
    }
}

If you are calling javac from within the same directory as both java files, you should not declare a package at the top, as they are within the 'default package'. As such, the above should work.

如果您在与两个 java 文件相同的目录中调用 javac,则不应在顶部声明包,因为它们位于“默认包”中。因此,上述应该有效。

Using the default package provides some convenience but also some restrictions which I won't elaborate on, but now that you know about the default package you can look it up. I recommend using package names, which is as simple as adding, as you did, something like:

使用默认包提供了一些便利,但也有一些限制,我不会详细说明,但是现在您知道默认包,您可以查找它。我建议使用包名,就像添加一样简单,就像你所做的那样:

package kugathasan;

at the beginning of each file. If you do this though, you should put both files in a directory called kugathasan and call javac from the directory containing kugathasan.

在每个文件的开头。如果你这样做,你应该把这两个文件放在一个名为 kugathasan 的目录中,并从包含 kugathasan 的目录中调用 javac。

回答by Sri Harsha Chilakapati

It is a package structure issue. You may understand by seeing this screenshot how they look in my IDE and in folder.

这是一个包结构问题。通过查看此屏幕截图,您可能会了解它们在我的 IDE 和文件夹中的外观。

enter image description here

在此处输入图片说明

And in the folder, they look like this.

在文件夹中,它们看起来像这样。

enter image description here

在此处输入图片说明

And in code, they have this package.

在代码中,他们有这个包。

package com.jini;

Hope you get the idea how this works.

希望你知道这是如何工作的。

回答by JDJ

technocrat's comment is correct.

技术官僚的评论是正确的。

In Java, you don't normally work directly with the .class files when developing your application. You work with the .java files. The packages that you create in your IDE should contain your .java files.

在 Java 中,您通常不会在开发应用程序时直接使用 .class 文件。您使用 .java 文件。您在 IDE 中创建的包应包含您的 .java 文件。

So instead of putting Pair.class inside the same package directory as fileA.java, put Pair.java in it. You should then have a directory/package that contains both fileA.java and Pair.java

因此,与其将 Pair.class 放在与 fileA.java 相同的包目录中,不如将 Pair.java 放在其中。然后你应该有一个包含 fileA.java 和 Pair.java 的目录/包