Java 打印对象的属性
我无法在这个代码中获得某张卡的属性,它总是打印Java 打印对象的属性,java,Java,我无法在这个代码中获得某张卡的属性,它总是打印null,我不知道为什么。我来自Php HTML世界,是JAVA新手 这是fromOpportunity.java package opportunity; import java.io.*; import java.util.Collections; import java.util.List; import static java.util.stream.Collectors.toList; import java.util.stream.Int
null
,我不知道为什么。我来自Php HTML世界,是JAVA新手
这是fromOpportunity.java
package opportunity;
import java.io.*;
import java.util.Collections;
import java.util.List;
import static java.util.stream.Collectors.toList;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import opportunity.Card.CardType;
public class Opportunity {
/*Player 1 */
public int p1_money = 10000;
public int p1_card_d = 40;
public int p1_card_h = 0;
/*Player 2 */
public int p2_money = 10000;
public int p2_card_d = 40;
public int p2_card_h = 0;
/**
* @param args the command line arguments
* @throws java.io.IOException
*/
public static void main(String[] args) throws IOException {
System.out.print("***************Opportunity***************\n");
final Card card1 = new Card(Card.CardType.EVENT, "Get Tax Returns").
setProperty("cost", "0.00").
setProperty("Effect", "Effect: Earn money equal to the\n"
+ "maximum income each of your\n"
+ "properties can give you,\n"
+ "depending on their level.");
final Card card2 = new Card(Card.CardType.EVENT, "BIR Hunting Begins").
setProperty("cost", "0.00").
setProperty("Effect", "Effect: An opponent loses\n"
+ "money equal to 50% of the\n"
+ "maximum income each of\n"
+ "their properties can give him or her,\n"
+ "depending on the level of the\n"
+ "property.");
final Card card3 = new Card(Card.CardType.EVENT, "Restore Balance").
setProperty("cost", "10000.00").
setProperty("Effect", "Effect: The total income of all\n"
+ "the players becomes equal to\n"
+ "the income of the player\n"
+ "with the lowest income.");
final List<Card> deck = Stream.of(CardType.values()).
flatMap(type -> IntStream.rangeClosed(1, 4).mapToObj(num -> new Card(type, "CardName" + num))).
collect(toList());
Collections.shuffle(deck);
System.out.println(deck.get(0).getProperty("Effect"));
}
}
package opportunity;
import java.util.*;
public class Card {
public enum CardType {
EVENT,
PROPERTY,
ASSET;
}
private final CardType cardType;
private final String cardName;
private final Map<String, String> properties = new HashMap<>();
Card(final CardType cardType, final String cardName) {
this.cardType = cardType;
this.cardName = cardName;
}
public Card setProperty(final String name, final String value) {
properties.put(name, value);
return this;
}
public String getProperty(final String name) {
return properties.get(name);
}
}
它总是返回以下内容:
run:
***************Opportunity***************
null
BUILD SUCCESSFUL (total time: 0 seconds)
即使我在get()
上放了一个索引,这里:
final List<Card> deck = Stream.of(CardType.values()).
flatMap(type -> IntStream.rangeClosed(1, 4).mapToObj(num -> new Card(type, "CardName" + num))).
collect(toList());
Collections.shuffle(deck);
final List deck=Stream.of(CardType.values())。
flatMap(type->IntStream.rangeClosed(1,4).mapToObj(num->newcard(type,“CardName”+num)))。
收集(toList());
收藏。洗牌(牌组);
您使用新的
卡
填充列表组
,不添加任何其他cardX
变量。因此,当检索此“Effect”
属性时,您会得到null
,因为牌组中的卡没有以前设置的“Effect”
属性。要基于@Luiggi Mendoza的答案,您没有将卡对象添加到牌组中
替换
final List<Card> deck = Stream.of(CardType.values()).
flatMap(type -> IntStream.rangeClosed(1, 4).mapToObj(num -> new Card(type, "CardName" + num))).
collect(toList());
这将把你的三张卡片中的每一张都增加四份。前面的人的评论完全正确。您没有将创建的卡添加到列表中
尝试替换您的代码
final List<Card> deck = Stream.of(CardType.values()).
flatMap(type -> IntStream.rangeClosed(1, 4).mapToObj(num -> new Card(type, "CardName" + num))).
collect(toList());
final List deck=Stream.of(CardType.values())。
flatMap(type->IntStream.rangeClosed(1,4).mapToObj(num->newcard(type,“CardName”+num)))。
收集(toList());
用于创建和添加标准列表。例如(并更新了4张卡):
List deck=newarraylist();
对于(int i=0;i<4;i++){
甲板。添加(card1);
甲板。添加(卡片2);
甲板。添加(card3);
}
如果您想尽量减少代码行数,可以通过以下方法初始化包含内容的新列表。试试这个帖子
无论如何,始终尝试编写最简单的解决方案,甚至在开始使用新语言时更是如此。请简要描述您的代码,并说明您试图解决问题的方法。看起来您并不是在向卡片组中添加卡片。我不确定你的lambda函数想做什么,但只需将添加到数组列表
中就更容易了。但是你要我替换的那一个会创建4个卡副本。因此,总共必须有12张卡,4张卡1
,4张卡2
,4张卡3
。您要我替换的代码创建12张卡来制作一个卡组。4卡1、4卡2、4卡3。
final List<Card> deck = new ArrayList<>();
for (int i=0; i<4; i++) {
deck.add(card1);
deck.add(card2);
deck.add(card3);
}
final List<Card> deck = Stream.of(CardType.values()).
flatMap(type -> IntStream.rangeClosed(1, 4).mapToObj(num -> new Card(type, "CardName" + num))).
collect(toList());
List<Card> deck = new ArrayList<Card>();
for (int i = 0; i < 4; i++){
deck.add(card1);
deck.add(card2);
deck.add(card3);
}