[英]Java package how to access a class in above folder
Here's how my directory look like: 这是我的目录的样子:
practice(folder)
GraphTester.java
graph(folder)
Digraph.java
algorithm(folder)
TopologicalSort.java
I want to use graph.Digraph
and graph.algorithm.TopologicalSort
from GraphTester.java
我想使用来自GraphTester.java
graph.Digraph
和graph.algorithm.TopologicalSort
What I try is this: 我尝试的是这样的:
package graph;
public class Digraph
{
...
}
package graph;
// package graph.algorithm; <-- also doesn't work
public class TopologicalSort
{
...
private Digraph graph; // doesn't work
}
My question is, how can I use Digraph
from inside TopologicalSort.java
? 我的问题是,如何从TopologicalSort.java
内部使用Digraph
?
==== Update=== ====更新===
I tried the following, but still not working 我尝试了以下操作,但仍无法正常工作
package graph;
//package graph.algorithm; <-- this also didn't work
import graph.Digraph;
public class TopologicalSort
{
...
private Digraph graph;
}
I updated how the directory look like above. 我更新了上面的目录。 My intention was to use GraphTester.java
as an outside class and not make it related to the package graph
and graph.algorithm
. 我的意图是将GraphTester.java
用作外部类,而不使其与程序包graph
和graph.algorithm
。 But, it seems like putting it under the folder practice
is causing the problem. 但是,似乎将其放在文件夹practice
会导致此问题。
Put import practice.graph.Digraph;
放import practice.graph.Digraph;
under your package declaration in TopologicalSort.java
. 在TopologicalSort.java
的包声明下。
Make sure the package declaration for TopologicalSort
is package practice.graph.algorithm;
确保了包声明TopologicalSort
是package practice.graph.algorithm;
, it must match the directory structure. ,它必须与目录结构匹配。
package statement is used to create a package. package语句用于创建一个包。
In order to use a package you need to use the import starement under the package statements follow by the name of the package you want to use. 为了使用软件包,您需要使用package语句下的import starement,后跟要使用的软件包的名称。
You can also "import inline" packages, just a way to use clases or interfaces without import is to write the full path when you use it. 您也可以“导入内联”程序包,一种使用clases或interfaces而不导入的方法是在使用时编写完整路径。
graph.algorithm.TopologicalSort ts = new graph.algorithm.TopologicalSort();
add import statements. 添加导入语句。
import practice.graph.Digraph; 进口惯例。图。图;
import practice.graph.algorithm.TopologicalSort; 导入惯例.graph.algorithm.TopologicalSort;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.