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
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
?
==== 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
. But, it seems like putting it under the folder practice
is causing the problem.
Put import practice.graph.Digraph;
under your package declaration in TopologicalSort.java
.
Make sure the package declaration for TopologicalSort
is package practice.graph.algorithm;
, it must match the directory structure.
package statement is used to create a 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.
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.
graph.algorithm.TopologicalSort ts = new graph.algorithm.TopologicalSort();
You can read the documentation here
add import statements.
import practice.graph.Digraph;
import practice.graph.algorithm.TopologicalSort;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.