java类在同一个包中找不到其他类 [英] A java class can't find an other in the same package
问题描述
我在ubuntu中实现了一个java程序,没有IDE将货币转换成€,我有两个类ConvertiEuro和Valuta都在同一个目录(包)中称为finanza,类ConvertiEuro使用类Valuta,当我尝试编译Valuta.java它正确编译,但当我编译ConvertiEuro.java时,我得到一个错误说ConvertiEuro.java:3:错误:找不到符号我不知道为什么这里是代码
I am implementing a java program in ubuntu without an IDE that converts a currency to €, i have 2 classes ConvertiEuro and Valuta both in the same directory (package) called finanza, the class ConvertiEuro uses the class Valuta, when i try to compile Valuta.java it compiles correctly but when i compile ConvertiEuro.java I get an error saying "ConvertiEuro.java:3: error: cannot find symbol" I don't know why here is the code
package finanza;
public class Valuta {
private String nomeValuta;
private double totValuta;
public Valuta(String nomeVal, double totVal) {
nomeValuta = nomeVal;
totValuta = totVal;
}
public String getNomeValuta() {
return nomeValuta;
}
public double getTotValuta() {
return totValuta;
}
}
package finanza;
import finanza.Valuta;
public class ConvertiEuro {
private int valuteGestibili;
private int cont = 0;
private Valuta [] valutas;
public ConvertiEuro(int valuteGest) {
this.valuteGestibili = valuteGest;
this.valutas = new Valuta [this.valuteGestibili];
}
public boolean impostaValuta(Valuta val){
if(cont<valuteGestibili) {
this.valutas[cont] = val;
cont ++;
return true;
}
else {
return false;
}
}
}
以及我如何编译:javac ConvertiEuro.java
and this how I compile: javac ConvertiEuro.java
推荐答案
我强烈怀疑问题在于你的编译方式。
I strongly suspect the problem is in how you're compiling.
两者 ConvertiEuro.java
和 Valuta.java
应该在一个名为 finanza
的目录中,理想情况下应该从父目录编译,以便所有编译器都知道在哪里找到其他代码相同的包裹。对于名为期望在您当前所在的目录下的
。 finanza
目录中查找源文件> finanza
Both ConvertiEuro.java
and Valuta.java
should be in a directory called finanza
, and you should ideally compile from the parent directory, so that all the compiler knows where to find other code in the same package. It would expect to find a source file in a finanza
directory under the one you're currently in, for a package called finanza
.
最简单的方法是同时编译所有文件:
It's simplest just to compile all the files at the same time though:
javac finanza/*.java
..或者更好的是,使用IDE来管理这类事情。
... or better yet, use an IDE which will manage this sort of things for you.
这篇关于java类在同一个包中找不到其他类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!