我有问题,把物体射入可变的 s。 ListBuffer. 我很熟悉相关的APIC,并知道你通常使用+=或++=方法添加物体或 Objects。
我在网络支持下开展了一次卡片游戏,并有一个简单的问题,即在
// get the references and ensure that it are rally ListBuffers / Lists
val handCards: mutable.ListBuffer[ClientCard] = playerPanel.player.handCards
val chosenCards: List[ClientCard] = _chosenCards
// print the number of elements per list
println("number of hand cards: " + handCards.size)
println("number of chosen cards: " + chosenCards.size)
// append the chosen cards to the hand cards
println("append operation: " + handCards + " ++= " + chosenCards)
handCards ++= chosenCards
// print the number of hand cards again
println("number of hand cards: " + handCards.size)
因此,人们预计手工业的规模会随着所选卡的规模而增加。 但产出(格式)如下:
number of hand cards: 5
number of chosen cards: 2
append operation: ListBuffer(
rftg.card.Card$$anon$1@1304043,
rftg.card.Card$$anon$1@cb07ef,
rftg.card.Card$$anon$1@176086d,
rftg.card.Card$$anon$1@234265,
rftg.card.Card$$anon$1@dc1f04
) ++= List(
rftg.card.Card$$anon$1@1784427,
rftg.card.Card$$anon$1@c272bc
)
number of hand cards: 5
因此,这些内容没有附后。
客户 贺卡一向是“真实卡”的代表,只包括提取卡所需的信息。
trait ClientCard extends AnyRef with ClientObject with CardLike
trait ClientObject extends Serializable {
def uid: Int
}
trait CardLike {
val imagePath: String
}
客户 贺卡在卡级:
def clientCard = new ClientCard() {
val uid = Card.this.hashCode()
val imagePath = CardTemplate.cardFolder + Card.this.imageFilename
}
还有一个客户(一个“真实角色”的代表)创建名单B公司:
// definition of ClientPlayer trait
trait ClientPlayer extends ClientObject {
val victoryPoints: Int
val handCards: mutable.ListBuffer[ClientCard]
val playedCards: mutable.ListBuffer[ClientCard]
}
// piece of code to create a client player
def clientPlayer = new ClientPlayer() {
val uid = Player.this.hashCode()
val victoryPoints = Player.this.victoryPoints
val handCards = new mutable.ListBuffer[ClientCard]
handCards ++= (Player.this.handCards.map(_.clientCard))
val playedCards = new mutable.ListBuffer[ClientCard]
playedCards ++= Player.this.playedCards.map(_.clientCard)
}
谁知道这里有什么错误? 或者说更笼统:有哪些情况可以防止将物体成功射入名单Buffer?
Edit: There is something that I forgot to mention and what seemed to cause this strange behaviour. After creation of the handCards ListBuffer it is being sent over network and is therefore being serialized and deserialized again.
在Rex Kerr发表评论后,我试图为客户Player制定深入细微的方法,并在对用户Player进行检索后复制。 这个问题已经解决。 是否有人对此行为作出解释?