简体   繁体   English

在我的程序中引用另一个.java文件中的类

[英]Refer class in another .java file in my program

I am absolutely new to Java and having trouble referencing a class that I have declared in another .java file. 我是Java的新手,无法引用我在另一个.java文件中声明的类。 I read a lot about it on the Oracle Website and also many stackoverflow questions, but couldn't solve the problem. 我在Oracle网站上阅读了很多关于它的内容以及许多stackoverflow问题,但无法解决问题。 Here are the steps I have taken: 以下是我采取的步骤:

Set the class path. 设置类路径。 I am running this on UNIX. 我在UNIX上运行它。 So I typed in % java -classpath /users/myUserName/algs4/assignment1 . 所以我输入% java -classpath /users/myUserName/algs4/assignment1 Instead of changing the class path it gives me a long list of other options. 它不是改变类路径,而是给我一长串的其他选项。

Include the 'package name' in each of my .java files which have class definitions I want to refer to. 在我的每个.java文件中包含'package name' ,它包含我想要引用的类定义。

I also ensured that all my files that I want to refer are in the same directory. 我还确保我想要引用的所有文件都在同一目录中。 Here is some of my code: 这是我的一些代码:

package assignment1;
public class Percolation
{
    private int[][] grid;
    ....
    //Other method definitions etc.
   public Percolation(int N)
   {
      uf = new WeightedQuickUnionUF(N);
      grid = new int[N][N];
      ...
      //Other code

   }

 }

My WeightedQuickUnionUF class is also defined as: 我的WeightedQuickUnionUF类也定义为:

package assignment1;
public class WeightedQuickUnionUF {
    private int[] id;    // id[i] = parent of i
    private int[] sz;    // sz[i] = number of objects in subtree rooted at i
    private int count;   // number of components

    //Create an empty union find data structure with N isolated sets.
    public WeightedQuickUnionUF(int N) {
        count = N;
        id = new int[N];
        sz = new int[N];
        for (int i = 0; i < N; i++) {
            id[i] = i;
            sz[i] = 1;
        }
     }
}

But now when I compile using javac Percolation.java. 但是现在当我使用javac Percolation.java进行编译时。 It gives me the errors: 它给了我错误:

Percolation.java:7: cannot find symbol
symbol  : class WeightedQuickUnionUF
location: class assignment1.Percolation
WeightedQuickUnionUF uf;
^

I get it that it is because the compiler doesn't know what is the class WeightedQuickUnionUF. 我明白这是因为编译器不知道什么是WeightedQuickUnionUF类。 It cannot refer it. 它不能参考它。 But how do I do it then? 但是我该怎么做呢? I have already tried the popular solutions. 我已经尝试了流行的解决方案。

I am not using any IDE. 我没有使用任何IDE。 Just a text editor and then compiling it on the terminal. 只需一个文本编辑器,然后在终端上编译它。

(algs4 folder has files like stdlib.jar and others) (algs4文件夹包含stdlib.jar等文件)

EDIT: I missed the 'new' keyword. 编辑:我错过了'新'关键字。 I have added that. 我补充说。 Also I am compiling my WeightedQuickUnionUF class before Percolation.java 我也在Percolation.java之前编译我的WeightedQuickUnionUF类

The java command is not setting the classpath but trying to run a java class. java命令不是设置类路径,而是尝试运行java类。 That's why the long option list. 这就是长选项列表的原因。

You can specify the classpath upon compilation: run javac -classpath . assignment1/Percolation.java 您可以在编译时指定类路径:运行javac -classpath . assignment1/Percolation.java javac -classpath . assignment1/Percolation.java from the algs4 directory. 来自algs4目录的javac -classpath . assignment1/Percolation.java Pay attention to the dot it means current directory. 注意它意味着当前目录的点。

There is no need to compile the files in order. 无需按顺序编译文件。

To run your program: java -cp . assignment1.Percolation 运行程序: java -cp . assignment1.Percolation java -cp . assignment1.Percolation from the algs4 directory. java -cp . assignment1.Percolation从algs4目录。

You need to first compile the dependencies, ie, in your case the WeightedQuickUnionUF class. 您需要首先编译依赖项,即在您的情况下编译WeightedQuickUnionUF类。

And if you want an anonymous object, declare it along with the new keyword : 如果您想要一个匿名对象,请将其与new关键字一起声明:

uf = new WeightedQuickUnionUF(N);

Or use it as mentioned in another answer. 或者在另一个答案中提到它。

Since that answer is deleted(I don't see it anymore), the other way around is to have a named object: 由于该答案被删除(我不再看到它),另一种方法是拥有一个命名对象:

WeightedQuickUnionUF myObj; 

.. ..

myObj = new WeightedQuickUnionUF(N);
uf = myObj;

You must compile the classes in the 你必须在中编译类

order of the flow of the Object Graph.

That means Percolation class CANNOT be compiled before the WeightedQuickUnionUF get compiled. 这意味着在WeightedQuickUnionUF编译之前 不能编译Percolation类

So first compile the WeightedQuickUnionUF then Percolation. 首先编译WeightedQuickUnionUF然后编译Percolation。

I think you have even missed out the new keyword...while initializing WeightedQuickUnionUF(N); 我认为您在初始化WeightedQuickUnionUF(N)时甚至错过了new关键字...

uf = new WeightedQuickUnionUF(N);

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

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