什么是Groovy等效于Python的dir()? [英] What's the Groovy equivalent to Python's dir()?

查看:171
本文介绍了什么是Groovy等效于Python的dir()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  print dir(my_object)

Groovy中的等价物(假设它有一个)?

解决方案

在Groovy中看起来非常好(未经测试,,所以代码积分应该去那里):

  //自省,知道所有有关类的细节:
//列出一个类的所有构造函数
String.constructors.each {println it}

//列出由类实现的所有接口
String.interfaces.each {println it}

//列出班级提供的所有方法
String.methods.each {println it}

/ /只列出方法名称
String.methods.name

//获取对象的字段(及其值)
d = new Date()
d .properties.each {println it}

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

In Python I can see what methods and fields an object has with:

print dir(my_object)

What's the equivalent of that in Groovy (assuming it has one)?

Looks particulary nice in Groovy (untested, taken from this link so code credit should go there):

// 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}

The general term you are looking for is introspection.

这篇关于什么是Groovy等效于Python的dir()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆