2012-01-14 103 views
0

我想在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) 

有沒有辦法,我可以重寫ToString運營商一樣,你可以在Java中,以實現任何方式這還是我做的方法,如:

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

並調用

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

回答

8
override def toString = "balance " + balance + " Player number " + player_num 
相關問題