getMethods()
方法getMethods()方法在java.lang包中可用。
getMethods()方法用于返回一个Method对象数组,这些对象指示该类或接口的所有公共方法,以及它包括从父类或父接口继承的所有公共方法。
getMethods()方法是一个非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。
返回一个Method对象数组时,getMethods()方法可能会引发异常。
SecurityException:在此异常中,当安全管理器存在时可能会引发此异常。
语法:
public Method[] getMethods();
参数:
它不接受任何参数。
返回值:
此方法的返回类型为Method [],它返回一个Method对象数组,该数组表示该类的所有公共方法,包括由此Class对象表示的所有公共继承的Method。
注意:
当类或接口不包含公共方法时,将返回0。
当此类对象包含基本类型,数组类或void类型时,将返回0。
示例
// Java program to demonstrate the example //Method [] getMethods() //类的方法 import java.lang.reflect.*; public class GetMethodsOfClass { public static void main(String[] args) throws Exception { Thread th = new Thread(); //获取String的Class对象 Class cl = th.getClass(); //调用线程类方法 Method[] methods = cl.getMethods(); for (int i = 0; i < methods.length; ++i) { System.out.print("String Class Methods = "); System.out.println(methods[i].toString()); } } }
输出结果
String Class Methods = public void java.lang.Thread.run() String Class Methods = public java.lang.String java.lang.Thread.toString() String Class Methods = public boolean java.lang.Thread.isInterrupted() String Class Methods = public static native java.lang.Thread java.lang.Thread.currentThread() String Class Methods = public static void java.lang.Thread.onSpinWait() String Class Methods = public final java.lang.String java.lang.Thread.getName() String Class Methods = public final void java.lang.Thread.join() throws java.lang.InterruptedException String Class Methods = public final synchronized void java.lang.Thread.join(long,int) throws java.lang.InterruptedException String Class Methods = public final synchronized void java.lang.Thread.join(long) throws java.lang.InterruptedException String Class Methods = public final java.lang.ThreadGroup java.lang.Thread.getThreadGroup() String Class Methods = public void java.lang.Thread.setContextClassLoader(java.lang.ClassLoader) String Class Methods = public java.lang.StackTraceElement[] java.lang.Thread.getStackTrace() String Class Methods = public static native boolean java.lang.Thread.holdsLock(java.lang.Object) String Class Methods = public final void java.lang.Thread.checkAccess() String Class Methods = public static void java.lang.Thread.dumpStack() String Class Methods = public final void java.lang.Thread.setPriority(int) String Class Methods = public final void java.lang.Thread.setDaemon(boolean) String Class Methods = public synchronized void java.lang.Thread.start() String Class Methods = public static native void java.lang.Thread.yield() String Class Methods = public static void java.lang.Thread.sleep(long,int) throws java.lang.InterruptedException String Class Methods = public static native void java.lang.Thread.sleep(long) throws java.lang.InterruptedException String Class Methods = public final void java.lang.Thread.stop() String Class Methods = public void java.lang.Thread.interrupt() String Class Methods = public static boolean java.lang.Thread.interrupted() String Class Methods = public final native boolean java.lang.Thread.isAlive() String Class Methods = public final void java.lang.Thread.suspend() String Class Methods = public final void java.lang.Thread.resume() String Class Methods = public final int java.lang.Thread.getPriority() String Class Methods = public final synchronized void java.lang.Thread.setName(java.lang.String) String Class Methods = public static int java.lang.Thread.activeCount() String Class Methods = public static int java.lang.Thread.enumerate(java.lang.Thread[]) String Class Methods = public native int java.lang.Thread.countStackFrames() String Class Methods = public final boolean java.lang.Thread.isDaemon() String Class Methods = public java.lang.ClassLoader java.lang.Thread.getContextClassLoader() String Class Methods = public static java.util.Map java.lang.Thread.getAllStackTraces() String Class Methods = public long java.lang.Thread.getId() String Class Methods = public java.lang.Thread$State java.lang.Thread.getState() String Class Methods = public static void java.lang.Thread.setDefaultUncaughtExceptionHandler(java.lang.Thread$UncaughtExceptionHandler) String Class Methods = public static java.lang.Thread$UncaughtExceptionHandler java.lang.Thread.getDefaultUncaughtExceptionHandler() String Class Methods = public java.lang.Thread$UncaughtExceptionHandler java.lang.Thread.getUncaughtExceptionHandler() String Class Methods = public void java.lang.Thread.setUncaughtExceptionHandler(java.lang.Thread$UncaughtExceptionHandler) String Class Methods = public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException String Class Methods = public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException String Class Methods = public final void java.lang.Object.wait() throws java.lang.InterruptedException String Class Methods = public boolean java.lang.Object.equals(java.lang.Object) String Class Methods = public native int java.lang.Object.hashCode() String Class Methods = public final native java.lang.Class java.lang.Object.getClass() String Class Methods = public final native void java.lang.Object.notify() String Class Methods = public final native void java.lang.Object.notifyAll()