繁体   English   中英

如何从另一个 Package 和 Java 中的文件夹导入类?

[英]How Do I Import Classes From Another Package and Folder in Java?

我刚开始研究 OOP 和 Java 中的 Packages。 我对在 Java 中导入 package 有疑问。

我有两个文件,名为ImportThis.javaHere.java

我本地机器上ImportThis.java的目录是F:\VS Codes\master\folderone\folderoneone\ImportThis.java ImportThis.java的内容是:

package master.folderone.folderoneone;

public class ImportThis {
    public static void aStaticMethod() {
        System.out.println("Hello World");
    }
}

Here.java在我的本地机器上的目录是F:\VS Codes\master\foldertwo\foldertwotwo\Here.java Here.java的内容是:

package master.foldertwo.foldertwotwo;

public class Here {
    public static void anotherMethod() {
        ImportThis.aStaticMethod();
    }
}

By looking at the contents of Here.java , you might be able to tell that I want to import the class ImportThis from ImportThis.java to Here.java , and it is indeed what I've been trying to do. 但是ImportThis.javaHere.java都来自不同的文件夹和包。 我试过使用import master.folderone.folderoneone.ImportThis; Here.java但 VS Code 说它无法解决。 期待我的问题的答案!

编辑:更改 package 名称并小写文件夹名称

尝试:

package master.foldertwo.foldertwotwo;

import master.folderone.folderoneone.ImportThis; //<-import statement

public class Here {
    public static void anotherMethod() {
        ImportThis.aStaticMethod();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM