什么是Groovy相当于Python的dir()?

Noe*_*Yap 9 python groovy

在Python中,我可以看到对象有哪些方法和字段:

print dir(my_object)
Run Code Online (Sandbox Code Playgroud)

什么是Groovy中的相同(假设它有一个)?

Chr*_*heD 8

在Groovy中看起来特别好看(未经测试,取自此链接,因此代码信用应该去那里):

// Introspection, know all the details about classes :
// List all constructors of a class
String.constructors.each{println it}

// List all interfaces implemented by a class
String.interfaces.each{println it}

// List all methods offered by a class
String.methods.each{println it}

// Just list the methods names
String.methods.name

// Get the fields of an object (with their values)
d = new Date()
d.properties.each{println it}
Run Code Online (Sandbox Code Playgroud)

你正在寻找的一般术语是内省.


bba*_*a42 5

如上所述这里,找到字符串对象定义的所有方法:

 "foo".metaClass.methods*.name.sort().unique()
Run Code Online (Sandbox Code Playgroud)

它不像Python版本那么简单,也许其他人可以展示出更好的方式.