在构造函数中向 ArrayList 添加新对象(Java)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19456860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 17:22:52  来源:igfitidea点击:

Adding new objects to an ArrayList in constructor (Java)

javaclassobjectarraylist

提问by chris

I am trying to add newly created objects to an ArrayList in the constructor of a class. The new objects are being created in another class in the main method.

我正在尝试将新创建的对象添加到类的构造函数中的 ArrayList。新对象是在 main 方法的另一个类中创建的。

Main method:

主要方法:

public static void main(String[] args) {
    // TODO code application logic here
    Player p1 = new Player("Peter");

}

}

My Player class:

我的播放器类:

public class Player {

protected static int age;
protected static String name;
protected static ArrayList players = new ArrayList();

Player(String aName) {

    name = aName;
    age = 15;
    players.add(new Player()); // i know this doesn't work but trying along these lines

   }
}

Any help much appreciated.

非常感谢任何帮助。

采纳答案by shikjohari

You have to edit the line

您必须编辑该行

players.add(new Player());

to

players.add(this);

Also, there is no need to make the age and name static

此外,无需将年龄和名称设为静态

I suggest you should have the following code instead

我建议你应该使用以下代码

import java.util.ArrayList;

public class Player {

protected int age;    //static is removed
protected String name;  // static is removed
protected static ArrayList<Player> players = new ArrayList<Player>();  //this is not a best practice to have a list of player inside player.

Player(String aName) {

    name = aName;
    age = 15;
    players.add(this); // i know this doesn't work but trying along these lines

   }


public static void main(String[] args) {
    // TODO code application logic here
    Player p1 = new Player("Peter");
}


}

回答by SLaks

It sounds like you're actuallyasking how to refer to the instance you're constructing.
That's what the thiskeyword does.

听起来您实际上是在询问如何引用您正在构建的实例。
这就是this关键字的作用。

回答by Haruki

This post is old, but for someone with similiar needs I suggest doing it like this:

这篇文章很旧,但对于有类似需求的人,我建议这样做:

Main class:

主要类:

public static void main(String[] args) {
    //TODO code application logic here
    java.util.List<Player> players = new java.util.ArrayList<>(); //list to hold all players
    Player p1 = new Player("Peter"); //test player
    players.add(p1); //adding test player to the list
    players.add(new Player("Peter")); //also possible in this case to add test player without creating variable for it.
}

Player class:

球员等级:

public class Player {

    private final int age = 15; //if you always going to use 15 as a age like in your case it could be final. if not then pass it to constructor and initialize the same way as the name.
    private String name;

    Player(String name) { //variable name can be the same as one in class, just add this prefix to indicated you are talking about the one which is global class variable.
        this.name = name;
    }

}

Generally you should avoid using static keyword on variables which supposed to be different in each instance of that class. Avoid having object of itself inside.

通常,您应该避免在该类的每个实例中应该不同的变量上使用 static 关键字。避免在里面有自己的对象。