【问题标题】:Adding new objects to an ArrayList in constructor在构造函数中将新对象添加到 ArrayList
【发布时间】:2013-10-27 17:19:54
【问题描述】:

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

主要方法:

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

我的播放器类:

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
    
   }
}

【问题讨论】:

  • nameage 不应是静态的。
  • 你可以试试players.add(this),它指的是你当前的实例,但我的建议是修改你的结构,因为我认为玩家不应该有责任记住到目前为止已经创建了哪些玩家.此功能应委托给专用实体
  • 可能有正当理由在 Player 中保留所有玩家的列表。我不是说我会这样做,但你可以像这样制作一个完美可行的程序
  • 我同意,总是有可能的。我不知道他的具体想法是什么,但是按照他的例子和静态变量的使用,我得出了上述结论
  • 它几乎看起来像一个使用ArrayList 而不是Player previous;Player next 的链表实现我敢打赌会有一个方法nextprevious。否则,我同意@Grove 的观点,即应该将其委托给其他地方

标签: java class object arraylist


【解决方案1】:

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

【讨论】:

  • 没有。他失败了,因为没有默认构造函数,new Player() 无法工作。
  • @DarthVader:是的,但无论如何这都没有意义。
  • @DarthVader 我认为 OP 正在使用 new Player() 来表达“this”的概念
  • 我认为那里存在设计和理解概念问题。比如静态道具,而玩家有玩家对我来说没有多大意义
【解决方案2】:

您必须编辑该行

players.add(new Player());

players.add(this);

另外,不需要将年龄和姓名设为静态

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

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");
}


}

【讨论】:

  • 仅添加一个默认构造函数会导致创建一个播放器,该播放器将 不同 新播放器添加到数组列表中,OP 使用 new Player() 来解释“这个”
  • 我最终设法做到了,而且完全按照这些思路。谢谢。
【解决方案3】:

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

主类:

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.
}

播放器类:

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;
    }

}

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

【讨论】:

    猜你喜欢
    • 2019-04-22
    • 2021-05-16
    • 2013-02-19
    • 2014-04-22
    • 1970-01-01
    • 2017-04-25
    • 1970-01-01
    • 2013-11-09
    • 2014-09-18
    相关资源
    最近更新 更多