2017-09-19 21 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

的方法