2012-06-04 35 views

回答

8

看起來在Groovy格外漂亮(未經測試,taken from this link所以代碼信貸應該去那裏):

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

你正在尋找的總稱是內省

+0

謝謝你提供這個詞! – WilliamShatner

4

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

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

它並不像Python版本那樣簡單,也許別人可以表現出更好的辦法。

相關問題