2017-09-19 19 views
0

這Python代碼Python的瓦爾()在科特林

class Test: 
    def __init__(self): 
    self.one = "python" 
    self.two = "is" 
    self.three = "fun!" 
t=Test() 
print(vars(t)) 

打印字段及其值的集合: {'one': 'python', 'two': 'is', 'three': 'fun!'} 我怎樣才能做到在科特林一樣嗎?

回答

2

如果您使用的是Koltin數據類,你可以只打印類

class Test(val one: String, val two: String, val three: String) 

fun main(args: Array<String>) { 
    val test = Test("Kotlin", "is", "fun") 
    println(test) 
} 

這將產生:Test(one=Kotlin, two=is, three=fun)

科特林數據類還提供了一個componentN()功能。在科特林

here有關數據類的一些信息,對於普通班,你可以嘗試的this answer

的方法