因此,我试图创建一个构建橄榄球队对象的类,但我不太确定数组实际上是如何作为字段工作的。我的构造是这样的:如何在其中一个字段是数组的类中创建构造函数(Java)
public class FootballTeam {
private String[] players;
private String colour;
private int goalDifference;
private Boolean challengeTrophy;
//---------- Construction and Initialisation of the FootballTeam object---------- //
public FootballTeam(String[] aPlayer, String aColour, int aGoalDifference, Boolean inChallengeTrophy) {
for (int i = 0; i < 5; i++) {players[i] = aPlayer[i]}
colour = aColour;
goalDifference = aGoalDifference;
challengeTrophy = inChallengeTrophy;
}
我真的不知道怎么去写构造器,这样我可以只使用一个字符串数组来测试玩家的一部分。
我还必须创建方法,允许添加或删除新玩家,我不确定我会如何构造它,任何帮助将不胜感激;干杯!
为什么不简单'玩家= aPlayer'?两者都是字符串类型的数组,因此您可以在没有for循环的情况下为它们分配 – mammago