可以使用java.lang.Class.getDeclaredMethods()方法获取所有已声明方法的列表。此方法返回一个数组,其中包含具有公共,私有,受保护和默认访问权限的所有Method对象。但是,不包括继承的方法。
另外,getDeclaredMethods()
如果类或接口没有方法,或者在Class对象中表示原始类型,数组类或void ,则该方法返回零长度数组。
演示此的程序如下所示-
import java.lang.reflect.Method; public class Main { public static void main(String[] argv) throws Exception { Class c = java.lang.String.class; Method[] methods = c.getDeclaredMethods(); for(int i = 0; i < methods.length; i++) { System.out.println("The method is: " + methods[i].toString()); } } }
输出结果
The method is: public boolean java.lang.String.equals(java.lang.Object) The method is: public java.lang.String java.lang.String.toString() The method is: public int java.lang.String.hashCode() The method is: public int java.lang.String.compareTo(java.lang.Object) The method is: public int java.lang.String.compareTo(java.lang.String) The method is: public int java.lang.String.indexOf(java.lang.String,int) The method is: static int java.lang.String.indexOf(char[],int,int,java.lang.String,int) The method is: static int java.lang.String.indexOf(char[],int,int,char[],int,int,int) The method is: public int java.lang.String.indexOf(int) The method is: public int java.lang.String.indexOf(java.lang.String) The method is: public int java.lang.String.indexOf(int,int) The method is: public static java.lang.String java.lang.String.valueOf(char) The method is: public static java.lang.String java.lang.String.valueOf(java.lang.Object) The method is: public static java.lang.String java.lang.String.valueOf(boolean) The method is: public static java.lang.String java.lang.String.valueOf(char[],int,int) The method is: public static java.lang.String java.lang.String.valueOf(char[]) The method is: public static java.lang.String java.lang.String.valueOf(double) The method is: public static java.lang.String java.lang.String.valueOf(float) The method is: public static java.lang.String java.lang.String.valueOf(long) The method is: public static java.lang.String java.lang.String.valueOf(int) The method is: private static void java.lang.String.checkBounds(byte[],int,int) The method is: public int java.lang.String.length() The method is: public boolean java.lang.String.isEmpty() The method is: public char java.lang.String.charAt(int) The method is: public int java.lang.String.codePointAt(int) The method is: public int java.lang.String.codePointBefore(int) The method is: public int java.lang.String.codePointCount(int,int) The method is: public int java.lang.String.offsetByCodePoints(int,int) The method is: public void java.lang.String.getChars(int,int,char[],int) The method is: void java.lang.String.getChars(char[],int) The method is: public byte[] java.lang.String.getBytes() The method is: public byte[] java.lang.String.getBytes(java.lang.String) throws java.io.UnsupportedEncodingException The method is: public void java.lang.String.getBytes(int,int,byte[],int) The method is: public byte[] java.lang.String.getBytes(java.nio.charset.Charset) The method is: public boolean java.lang.String.contentEquals(java.lang.StringBuffer) The method is: public boolean java.lang.String.contentEquals(java.lang.CharSequence) The method is: private boolean java.lang.String.nonSyncContentEquals(java.lang.AbstractStringBuilder) The method is: public boolean java.lang.String.equalsIgnoreCase(java.lang.String) The method is: public int java.lang.String.compareToIgnoreCase(java.lang.String) The method is: public boolean java.lang.String.regionMatches(int,java.lang.String,int,int) The method is: public boolean java.lang.String.regionMatches(boolean,int,java.lang.String,int,int) The method is: public boolean java.lang.String.startsWith(java.lang.String) The method is: public boolean java.lang.String.startsWith(java.lang.String,int) The method is: public boolean java.lang.String.endsWith(java.lang.String) The method is: private int java.lang.String.indexOfSupplementary(int,int) The method is: public int java.lang.String.lastIndexOf(int,int) The method is: static int java.lang.String.lastIndexOf(char[],int,int,char[],int,int,int) The method is: static int java.lang.String.lastIndexOf(char[],int,int,java.lang.String,int) The method is: public int java.lang.String.lastIndexOf(java.lang.String,int) The method is: public int java.lang.String.lastIndexOf(int) The method is: public int java.lang.String.lastIndexOf(java.lang.String) The method is: private int java.lang.String.lastIndexOfSupplementary(int,int) The method is: public java.lang.String java.lang.String.substring(int) The method is: public java.lang.String java.lang.String.substring(int,int) The method is: public java.lang.CharSequence java.lang.String.subSequence(int,int) The method is: public java.lang.String java.lang.String.concat(java.lang.String) The method is: public java.lang.String java.lang.String.replace(char,char) The method is: public java.lang.String java.lang.String.replace(java.lang.CharSequence,java.lang.CharSequence) The method is: public boolean java.lang.String.matches(java.lang.String) The method is: public boolean java.lang.String.contains(java.lang.CharSequence) The method is: public java.lang.String java.lang.String.replaceFirst(java.lang.String,java.lang.String) The method is: public java.lang.String java.lang.String.replaceAll(java.lang.String,java.lang.String) The method is: public java.lang.String[] java.lang.String.split(java.lang.String,int) The method is: public java.lang.String[] java.lang.String.split(java.lang.String) The method is: public static java.lang.String java.lang.String.join(java.lang.CharSequence,java.lang.CharSequence[]) The method is: public static java.lang.String java.lang.String.join(java.lang.CharSequence,java.lang.Iterable) The method is: public java.lang.String java.lang.String.toLowerCase(java.util.Locale) The method is: public java.lang.String java.lang.String.toLowerCase() The method is: public java.lang.String java.lang.String.toUpperCase() The method is: public java.lang.String java.lang.String.toUpperCase(java.util.Locale) The method is: public java.lang.String java.lang.String.trim() The method is: public char[] java.lang.String.toCharArray() The method is: public static java.lang.String java.lang.String.format(java.util.Locale,java.lang.String,java.lang.Object[]) The method is: public static java.lang.String java.lang.String.format(java.lang.String,java.lang.Object[]) The method is: public static java.lang.String java.lang.String.copyValueOf(char[],int,int) The method is: public static java.lang.String java.lang.String.copyValueOf(char[]) The method is: public native java.lang.String java.lang.String.intern()