【发布时间】:2019-09-23 15:58:06
【问题描述】:
您好,我的软件向导网络。我有一个问题,为什么我不能让我的课程编译。我不断收到错误:找不到符号。所有这些类都在同一个包块法中。我已经尝试了 import 语句的所有变体,但似乎无法消除此错误。目录结构是blocklaw --> src --> all_classes_here。 我也尝试更改包声明 blocklaw.src ,但仍然没有帮助。 我将在这里分享我的代码,
package blocklaw;
import java.security.MessageDigest;
/*
* returns a string value of the true fact
*/
public class TrueFact implements Hashable {
private String trueFact;
private String hashFact;
private String source;
private String hashSource;
private int bytesLength;
private String hashedBytesLength;
private String hashedHash;
public TrueFact(String trueFact, String source){
this.trueFact = trueFact;
this.source = new Source(source).getSource();
byte[] bytes = trueFact.getBytes("UTF-8");
this.bytesLength = bytes.length;
setHashCode();
}
public String getTrueFact(){
return this.trueFact;
}
public String getHashFact(){
return this.hashFact;
}
public String getSource(){
return this.source;
}
public int getbytesLength(){
return this.bytesLength;
}
public String getHashedBytesLength(){
return this.hashedBytesLength;
}
public void setHashCode(){
this.hashFact = hash(this.trueFact);
this.hashSource = hash(this.source);
this.hashedBytesLength = hash(String.valueOf(this.bytesLength));
this.hashedHash = hash(this.hashFact+this.hashSource+this.hashedBytesLength);
}
public String getHashSource(){
return this.hashSource;
}
public String hash(String input){
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
//Applies sha256 to our input,
byte[] hash = digest.digest(input.getBytes("UTF-8"));
StringBuffer hexString = new StringBuffer(); // This will contain hash as hexidecimal
for (int i = 0; i < hash.length; i++) {
String hex = Integer.toHexString(0xff & hash[i]);
if(hex.length() == 1) hexString.append('0');
hexString.append(hex);
}
return hexString.toString();
}
catch(Exception e) {
throw new RuntimeException(e);
}
}
}
据我了解,同一个包中的所有类都不需要显式导入。那么为什么我的错误导致它找不到符号?
下面是Hashable的接口...
package blocklaw;
public interface Hashable{
public String hash();
}
【问题讨论】:
-
你知道哪个班级,你得到这个错误。那堂课是私人的吗?
-
就是这个类。我正在尝试编译,所以我可以测试这个类。该接口是公共的,具有一个公共类 hash()。
-
您的代码应该可以编译,如果它们在您的类的同一个包中,则无需专门
import公共类(或受保护或包私有)。但是你究竟是如何编译这段代码的——看起来你使用javac?对了,你用什么命令? -
以上代码中哪一行导致编译失败?
-
Daniele...好问题我正在使用$:javac TrueFact.java