1

我正在尝试在 scala 中打印一个类。定义如下:

class Player(balance : Int, player_num :Int ){
    var curHand = new ArrayBuffer[Int]()
    var sum = 0

    def method1()={
        ....
    }
}

我想通过以下方式打印到控制台:

var player1 = new Player(100,1)
println(player1)

有什么方法可以像在 java 中那样覆盖 toString 运算符以实现此目的,还是我必须制作如下方法:

def print(){
    println("balance " + balance + "Player number" + player_num)
}

并打电话

var player1 = new Player(100,1)
player1.print()
4

1 回答 1

9
override def toString = "balance " + balance + " Player number " + player_num
于 2012-01-14T01:57:42.103 回答