使用inspect方法得到Groovy对象信息

10 views
Skip to first unread message

foxgem

unread,
Dec 20, 2009, 6:28:48 AM12/20/09
to groovy-xa
Hubert Klein Ikkink的新贴给出了inspect方法的例子,多余的话不要说了,代码即说明的一切:
 
def map = [username: 'mrhaki']
assert '["username":"mrhaki"]' == map.inspect()
 
def list = [1, 2, 3, 'Groovy']
assert '[1, 2, 3, "Groovy"]' == list.inspect()
 
def range = 0..10
assert '0..10' == range.inspect()
 
def str = 'Inspecting object with Groovy'
assert '"Inspecting object with Groovy"' == str.inspect()
 
def dom = groovy.xml.DOMBuilder.newInstance().parseText('<root><language>Groovy</language></root>')
println dom.documentElement.inspect()
// Output:
// <?xml version="1.0" encoding="UTF-8"?>
// <root>
//   <language>Groovy</language>
// </root>
对于inspect方法:which will try to return a String which matches closest what we would type in Groovy code to create the object.
 
Reply all
Reply to author
Forward
0 new messages