Java从我创建的另一个类访问数组元素 [英] Java access an array element from another class I created

查看:38
本文介绍了Java从我创建的另一个类访问数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用main方法在类中创建一个数组

I am creating an array in the class with main method

Word attempts = new Word (26);

Word类中的字段是

Field in class Word is

private String [] attempts;

Word类中的构造函数是

Constructor in class Word is

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

其中create是一个使每个数组元素都为空字符串(")的方法.在Word类中,我还有一个 getAttempts()方法用于访问trys数组.现在,我要使Letter类在for循环中传递之前创建的数组 Word [] .我尝试使用Word.getAttempts()[i],但出现错误无法从类型Word 静态引用非静态方法getAttempts().据我了解,当一个方法是静态的时,您不必在调用该方法之前创建一个对象.我不知道如何将在Main方法中创建的数组传递给此Letter类.有帮助吗?

Where create is a method that makes every array element an empty String (""). In class Word I've also got a getAttempts() method for accessing the attempts array. Now, I want to make class Letter where i pass the before created array Word [] in a for loop. 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. 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. Any help?

这是我的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类.

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.

推荐答案

Word.getAttempts()

...将是您访问类 Word 中名为 getAttempts 的静态方法的方式.但是,您的方法 getAttempts 不是静态的:它适用于您的类的实例.

... would be how you would access a static method named getAttempts in class Word. However, your method getAttempts is not static: it works on instances of your class.

假设您按如下方式定义此类实例:

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.

我了解这一点,但是在Main方法中定义了Word数组之后,如何在新类中访问它?

I understand that, but after defining Word array in Main method, how can i access it in a new class?

您通过方法或构造函数传递对象,这些方法或构造函数允许其他对象使用公共方法定义的API与之交互.

You pass objects through methods or constructors, which allow other objects to interact with it using the API defined by public methods.

现在,我想在传递[...]数组的地方制作Letter类.

Now, I want to make class Letter where i pass the [...] array

定义一个名为 Letter 的类,以及一个接受 Word 类的对象的构造函数.

Define a class named Letter, and a constructor which accept an object of class Word.

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

main 中:

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

您可以直接传递 word.getAttempts(),但是随后您直接使用另一个类的内部值,这是不好的样式.通过直接使用 Word 实例的公共方法,比直接访问其私有数据更好.

You could pass directly word.getAttempts(), but then you are directly working with the internal values of another class, which is bad style. Better work with instances of Word through its public methods than by directly accessing its private data.

这篇关于Java从我创建的另一个类访问数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆