简体   繁体   English

创建对象数组; 最好的方法是什么?

[英]Creating array of objects; Best way to do it?

Good afternoon.下午好。

I am attempting to make a small game inspired by Among Us.我正在尝试制作一个受我们中间启发的小游戏。 The game will feature two bandits if there are 7 or more players, and one bandit if the player count is 6 or less.如果玩家人数为 7 人或以上,游戏将有两个强盗,如果玩家人数为 6 人或更少,游戏将有一个强盗。

I have run into a problem though, so I am attempting to create multiple constructors, here is how I imagine the layout to be.我遇到了一个问题,所以我试图创建多个构造函数,这是我想象的布局。

Console -> Input names separated by comma -> String[] names -> newPlayer Constructor控制台 -> 以逗号分隔的输入名称 -> String[] 名称 -> newPlayer 构造函数

The newPlayer constructor will then determine the number of players, and sort them into their respective constructor, using Math.random to determine the bandits.然后 newPlayer 构造函数将确定玩家的数量,并将他们分类到各自的构造函数中,使用 Math.random 来确定强盗。

Bandit route newPlayer Constructor -> Bandit Constructor -> Player Constructor Bandit 路线 newPlayer Constructor -> Bandit Constructor -> Player Constructor

Cowboy route newPlayer Constructor -> Bandit Constructor -> Player Constructor牛仔路线 newPlayer Constructor -> Bandit Constructor -> Player Constructor

The problem is that after the data is given to the Player constructor, I'd like to create an array of type Player[], so I can easily access the data within, without creating invidual Player variables, and then making this array accessible to the rest of the classes within the package.问题是在将数据提供给 Player 构造函数之后,我想创建一个 Player[] 类型的数组,这样我就可以轻松访问其中的数据,而无需创建单独的 Player 变量,然后使该数组可访问包中的其余类。 I'm looking for the most concise way to do it.我正在寻找最简洁的方法来做到这一点。

Although I feel like this may not require code for context, I'll provide it in case it helps answer my question.虽然我觉得这可能不需要上下文代码,但我会提供它以防它有助于回答我的问题。

Bandit Class强盗级

    package Sorting;

public class Bandit {
    private int playerID;
    private String playerName;
public Bandit(int ID, String name) {
    this.playerName = name;
    this.playerID = ID;
    Player newBandit = new Player(playerID,true,playerName);
}
}

Cowboy Class牛仔班

package Sorting;

public class Cowboy {

    private int playerID;
    private String playerName;
    
    public Cowboy(int ID, String name) {
        this.playerID = ID;
        this.playerName = name;
        Player newCowboy = new Player(this.playerID,false,this.playerName);
    }
    
}

newPlayer Class新播放器类

package Sorting;

public class NewPlayer {
    private String name;
    private int playerID;
    private int impostor1, impostor2;
    
    NewPlayer (int ID, String name, int playerCount) {
        if(playerCount<=6) {int impostor1 = (int)Math.random()*playerCount;
        if(impostor1==this.playerID) {
        Bandit bandit1 = new Bandit (this.playerID, this.name);}
        }
        else {
            impostor1 = (int)Math.random()*playerCount;
            impostor2 = (int)Math.random()*playerCount;
            while (impostor1 == impostor2) {
                impostor2 = (int)Math.random()*playerCount;
            }
            if(impostor1==this.playerID) {
            Bandit bandit1 = new Bandit (this.playerID, this.name);}
            if(impostor2==this.playerID) {
            Bandit bandit2 = new Bandit (this.playerID, this.name);}
        }
        for(int i=0;i<playerCount;i++) {
            if ((this.playerID!=impostor1)&&(this.playerID!=impostor2)) {
                Cowboy newCowboy = new Cowboy(this.playerID,this.name);
            }
        }
        }
        
    }

Player Class球员等级

package Sorting;
public class Player {
    
    public int playerCount;
    private int playerID;
    private boolean playerBandit;
    private String playerName;
    
    public Player(int id, boolean bandit, String name) {
        this.playerID = id;
        this.playerBandit = bandit;
        this.playerName = name;
        playerCount++;
    }
    public String getPlayerName() {return this.playerName;}
    public boolean getBandit() {return this.playerBandit;}
    public int getPlayerID() {return this.playerID;}
}

I do not think the Tester class would be required to answer this question, but I'll provide it.我认为 Tester 类不需要回答这个问题,但我会提供它。 Be aware, this part is not finished.请注意,这部分还没有完成。

package Sorting;
import java.util.*;
public class Tester {
    private static int playerCount;
    public static void main(String[] args) {
        
        Scanner input = new Scanner(System.in);
        System.out.println("Please enter player names, separated by a comma: ");
        String nameList[] = input.nextLine().split(",");
        playerCount = nameList.length;
        Assigner.Assignment(nameList);
    }
    public int getPlayerCount() {return playerCount;}
}

I'd like the array of Players to be created within the tester class, and accessible to the rest of the classes within the package.我希望在 tester 类中创建 Players 数组,并且可以访问包中的其余类。

What is the best way to do this?做这个的最好方式是什么?

"I'd like the array of Players to be created within the tester class" : I would suggest using List interface instead of native java array (much easier to use). “我希望在测试器类中创建玩家数组” :我建议使用 List 接口而不是本机 java 数组(更容易使用)。 Something like this: List<Player> players = new ArrayList<>() and you can use players.add( /*player*/ ) and players.get(/*idx*/) to add and retrieve.像这样: List<Player> players = new ArrayList<>()并且您可以使用players.add( /*player*/ )players.get(/*idx*/)来添加和检索。

"accessible to the rest of the classes within the package" : a default class property is accessible for classes within the same package. “可访问包中的其余类” :默认类属性可用于同一包中的类。 Like this: List<Player> players;像这样: List<Player> players; or already initialized: List<Player> players = new ArrayList<>();或者已经初始化: List<Player> players = new ArrayList<>(); . . Don't use any modifier like private , public or protected and it will mean this property is "default" access (within same package).不要使用任何修饰符,如privatepublicprotected ,这意味着这个属性是“默认”访问(在同一个包中)。 See: https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html请参阅: https : //docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

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

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