在java的反射中,有两种方法可以获得某个类的所有方法或者只是public,protected,private方法:
1) 获得只是public,protected,private方法:
2) 获得所有方法
1) 获得只是public,protected,private方法:
public String[] getAllOwnMethodNames(){
ArrayList<String> allMethods = new ArrayList<String>();
for(Method aMethod : myClass.getDeclaredMethods()){
allMethods.add("Method Name : "+aMethod.getName()+" , Full Name : "+aMethod.toString());
}
return allMethods.toArray(new String[allMethods.size()]);
}
2) 获得所有方法
[/code],包括继承父类下来的:
[code="java"]
public String[] getAllPubliAndInheritedMethodNames(){
ArrayList<String> allMethods = new ArrayList<String>();
for(Method aMethod : myClass.getMethods()){
allMethods.add("Method Name : "+aMethod.getName()+" , Full Name : "+aMethod.toString());
}
return allMethods.toArray(new String[allMethods.size()]);
}