我需要一些帮助来弄清楚发生了什么。当我通过套接字发送一个对象时,其中一个属性有时会变为空,但其他属性也没问题。
public class ItsYourTurnInfo extends ServerInfo {
private static final long serialVersionUID = 1L;
private String testString;
private Hand hand;
private Suit suitLead;
public ItsYourTurnInfo(Hand hand, Suit suitLead, String testString) {
this.hand = hand;
this.suitLead = suitLead;
this.testString = testString;
System.out.println("It's your turn, hand size: " + this.hand.getSize());
System.out.println("Test String 1: " + testString);
}
@Override
public void execute(GameFrame gameFrame) {
System.out.println("It's your turn, hand size again: "+ this.hand.getSize());
System.out.println("Leading Suit: " + this.suitLead);
System.out.println("Test String 2: " + this.testString);
CardsPanel panel = gameFrame.getCardsPanel();
panel.updateHand(this.hand, this.suitLead);
}
}
我得到的结果是:
It's your turn, hand size: 29
Test String 1: This is just a test
It's your turn, hand size again: 0
Leading Suit: Warriors
Test String 2: This is just a test
有时候,第3行会显示“轮到你了,手牌大小:29” 我用来运行它的代码是在一个线程中,但我认为这不会导致问题:
class PlayGame extends Thread {
public void run() {
dealCards();
Player p = players.get(0);
System.out.println("Player hand size: " + p.getHand().getSize());
p.write(new ItsYourTurnInfo(p.getHand(), Suit.WARRIORS, "This is just a test"));
while (!gameOver && players.size() > 0) {
try {
Thread.sleep(500);
} catch (InterruptedException e) { /* Do nothing */
}
}
close();
}
}
Hand类是序列化的(因为它是ItsYourTurnInfo扩展的ServerInfo)。
此外,我正在一台计算机上运行此程序进行测试。尚未在物理网络上尝试过。 只有Hand属性受到影响(有时会有卡片,有时却没有),我无法理解为什么。感谢您提供的任何帮助。