【发布时间】:2018-07-13 22:34:01
【问题描述】:
我正在使用 main 方法在类中创建一个数组
Word attempts = new Word (26);
Word 类中的字段是
private String [] attempts;
Word 类中的构造函数是
public Word (int a){
attempts = new String [a];
create(attempts);
}
其中 create 是一种使每个数组元素成为空字符串 ("") 的方法。在 Word 类中,我还有一个 getAttempts() 方法来访问尝试数组。现在,我想在 for 循环中传递之前创建的数组 Word [] 来创建类 Letter。我尝试使用 Word.getAttempts()[i],但收到错误 Cannot make a static reference to the non-static method getAttempts() from the type Word。据我了解,当方法是静态的时,您不必在调用方法之前创建对象。我不知道如何将 Main 方法中创建的数组传递给这个 Letter 类。有什么帮助吗?
编辑:这是我的 Word 类
public class Word {
private String [] attempts;
public String[] getAttempts() {
return attempts;
}
public void create (String [] attempts){
for (int i=0; i<attempts.length; i++){
attempts[i]="";
}
}
public Word (int a){
attempts = new String [a];
create(attempts);
}
}
总而言之,我正在使用 Word 类型的 Main 方法在类中创建一个数组,我想将该数组传递给单独的类 Letter。
【问题讨论】:
-
消息说你的方法不是静态的......请插入你的类
-
@André 编辑了帖子,现在怎么样?
标签: java arrays class methods static