我有一个工作方便的构造函数,但我觉得它的代码太多了。我不知道如何简化它,但我会很感激任何帮助!
public Hand(Card c1, Card c2, Card c3, Card c4, Card c5, Card c6) {
this();
this.card1 = c1;
this.card2 = c2;
this.card3 = c3;
this.card4 = c4;
this.card5 = c5;
this.card6 = c6;
if (c1 != null && c2 != null && c3 != null && c4 != null && c5 != null && c6 != null) {
for (int count = 0; count < 6; count++) {
if (count == 0) {
cardsInHand.add(c1);
} else if (count == 1) {
cardsInHand.add(c2);
} else if (count == 2) {
cardsInHand.add(c3);
} else if (count == 3) {
cardsInHand.add(c4);
} else if (count == 4) {
cardsInHand.add(c5);
} else if (count == 5) {
cardsInHand.add(c6);
}
}
}
}
编辑:用下面的建议清理代码。该程序仍然使用以下代码运行:
public Hand(Card c1, Card c2, Card c3, Card c4, Card c5, Card c6) {
this();
this.card1 = c1;
this.card2 = c2;
this.card3 = c3;
this.card4 = c4;
this.card5 = c5;
this.card6 = c6;
cardsInHand.add(c1);
cardsInHand.add(c2);
cardsInHand.add(c3);
cardsInHand.add(c4);
cardsInHand.add(c5);
cardsInHand.add(c6);