简体   繁体   English

Java从我创建的另一个类中访问数组元素

[英]Java access an array element from another class I created

I am creating an array in the class with main method 我正在使用main方法在类中创建一个数组

Word attempts = new Word (26);

Field in class Word is 类Word中的字段是

private String [] attempts;

Constructor in class Word is 类Word中的构造函数是

public Word (int a){
        attempts = new String [a];
        create(attempts);
    }

Where create is a method that makes every array element an empty String ("") . 其中create是一个使每个数组元素都为空String ("") In class Word I've also got a getAttempts() method for accessing the attempts array. 在类Word中,我还有一个getAttempts()方法来访问attempts数组。 Now, I want to make class Letter where i pass the before created array Word [] in a for loop. 现在,我想创建类字母,我在for循环中传递之前创建的数组Word [] I tried with Word.getAttempts()[i], but I get an error Cannot make a static reference to the non-static method getAttempts() from the type Word . 我尝试使用Word.getAttempts()[i],但是我收到错误Cannot make a static reference to the non-static method getAttempts() from the type Word To my understanding when a method is static, you do not have to create an object before calling the method. 根据我的理解,当方法是静态的时,您不必在调用方法之前创建对象。 I have no idea how to pass the array created in Main method to this Letter class. 我不知道如何将Main方法中创建的数组传递给此Letter类。 Any help? 有帮助吗?

edit: Here is my Word class 编辑:这是我的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);
    }
}

To sum up, I am creating an array in class with Main method that is type of Word, and I want to pass that array to separate class Letter. 总而言之,我在类中使用Main类型创建一个数组,我希望将该数组传递给单独的类Letter。

Word.getAttempts()

... would be how you would access a static method named getAttempts in class Word . ...将是如何在类Word访问名为getAttempts的静态方法。 However, your method getAttempts is not static: it works on instances of your class. 但是,您的方法getAttempts不是静态的:它适用于您的类的实例。

Assuming you define such an instance as follows: 假设您定义了如下实例:

Word word = new Word(26);

Then, provided the method is public, you can access the array with: 然后,如果方法是公共的,您可以使用以下命令访问该数组:

String[] attempts = word.getAttempts();

To my understanding when a method is static, you do not have to create an object before calling the method. 根据我的理解,当方法是静态的时,您不必在调用方法之前创建对象。

Yes, but your method is not static. 是的,但你的方法不是静态的。


I understand that, but after defining Word array in Main method, how can i access it in a new class? 我理解,但在Main方法中定义Word数组后,如何在新类中访问它?

You pass objects through methods or constructors, which allow other objects to interact with it using the API defined by public methods. 您可以通过方法或构造函数传递对象,这些方法或构造函数允许其他对象使用公共方法定义的API与其进行交互。

Now, I want to make class Letter where i pass the [...] array 现在,我想在我传递[...]数组时制作类字母

Define a class named Letter , and a constructor which accept an object of class Word . 定义一个名为Letter的类,以及一个接受Word类对象的构造函数。

class Letter
{
    private final Word word;
    public Letter (Word word) {
        this.word = word;
    }
}

And in main : main

public static void main (String[] args)
{
    Word word = new Word(26) ;
    Letter letter = new Letter(word);
}

You could pass directly word.getAttempts() , but then you are directly working with the internal values of another class, which is bad style. 你可以直接传递word.getAttempts() ,但是你直接使用另一个类的内部值,这是一种糟糕的风格。 Better work with instances of Word through its public methods than by directly accessing its private data. 通过公共方法更好地处理Word实例,而不是直接访问其私有数据。

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

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