簡體   English   中英

什么是Groovy相當於Python的dir()?

[英]What's the Groovy equivalent to Python's dir()?

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

print dir(my_object)

什么是Groovy中的相同(假設它有一個)?

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

你正在尋找的一般術語是內省

如上所述這里 ,找到字符串對象定義的所有方法:

 "foo".metaClass.methods*.name.sort().unique()

它不像Python版本那么簡單,也許其他人可以展示出更好的方式。

除了使用普通的Java反射API之外,還有:

http://docs.codehaus.org/display/GROOVY/JN3535-Reflection

您還可以使用元類玩游戲。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM