📜  Java中的类 getMethods() 方法和示例

📅  最后修改于: 2022-05-13 01:54:58.905000             🧑  作者: Mango

Java中的类 getMethods() 方法和示例

Java.lang.Class 类getMethods()方法用于获取该类的方法,即公开的方法及其成员或者其成员类和接口的成员。该方法以 Method 对象数组的形式返回该类的方法。
句法:

public Method[] getMethods()

参数:此方法不接受任何参数。
返回值:该方法以Method对象数组的形式返回该类的方法
异常如果存在安全管理器并且不满足安全条件,则此方法将引发SecurityException
下面的程序演示了 getMethods() 方法。
示例 1:

Java
// Java program to demonstrate getMethods() method
 
import java.util.*;
 
public class Test {
    public static void main(String[] args)
        throws ClassNotFoundException
    {
 
        // returns the Class object for this class
        Class myClass = Class.forName("Test");
 
        System.out.println("Class represented by myClass: "
                           + myClass.toString());
 
        // Get the methods of myClass
        // using getMethods() method
        System.out.println("Methods of myClass: "
                           + Arrays.toString(
                                 myClass.getMethods()));
    }
}


Java
// Java program to demonstrate getMethods() method
 
import java.util.*;
 
class Main {
 
    public Object obj;
    private void function() {}
 
    Main()
    {
 
        class Arr {
        };
 
        obj = new Arr();
    }
 
    public static void main(String[] args)
        throws ClassNotFoundException
    {
        Main t = new Main();
 
        // returns the Class object
        Class myClass = t.obj.getClass();
 
        // Get the methods of myClass
        // using getMethods() method
        System.out.println("Methods of myClass: "
                           + Arrays.toString(
                                 myClass.getMethods()));
    }
}


输出:

myClass 代表的类:class Test
myClass 的方法:[
public static void Test.main(Java.lang.String[]) 抛出Java.lang.ClassNotFoundException,
public final void Java.lang.Object.wait(long, int) 抛出Java.lang.InterruptedException,
public final native void Java.lang.Object.wait(long) 抛出Java.lang.InterruptedException,
public final void Java.lang.Object.wait() 抛出Java.lang.InterruptedException,
公共布尔Java.lang.Object.equals(Java.lang.Object),
公共Java.lang.String Java.lang.Object.toString(),
公共原生 int Java.lang.Object.hashCode(),
public final 本机Java.lang.Class Java.lang.Object.getClass(),
public final native void Java.lang.Object.notify(),
public final native void Java.lang.Object.notifyAll()
]

示例 2:

Java

// Java program to demonstrate getMethods() method
 
import java.util.*;
 
class Main {
 
    public Object obj;
    private void function() {}
 
    Main()
    {
 
        class Arr {
        };
 
        obj = new Arr();
    }
 
    public static void main(String[] args)
        throws ClassNotFoundException
    {
        Main t = new Main();
 
        // returns the Class object
        Class myClass = t.obj.getClass();
 
        // Get the methods of myClass
        // using getMethods() method
        System.out.println("Methods of myClass: "
                           + Arrays.toString(
                                 myClass.getMethods()));
    }
}
输出:

myClass 的方法:[
public static void Test.main(Java.lang.String[]) 抛出Java.lang.ClassNotFoundException,
public final void Java.lang.Object.wait(long, int) 抛出Java.lang.InterruptedException,
public final native void Java.lang.Object.wait(long) 抛出Java.lang.InterruptedException,
public final void Java.lang.Object.wait() 抛出Java.lang.InterruptedException,
公共布尔Java.lang.Object.equals(Java.lang.Object),
公共Java.lang.String Java.lang.Object.toString(),
公共原生 int Java.lang.Object.hashCode(),
public final 本机Java.lang.Class Java.lang.Object.getClass(),
public final native void Java.lang.Object.notify(),
public final native void Java.lang.Object.notifyAll()
]

参考: https://docs.oracle.com/javase/9/docs/api/ Java/lang/Class.html#getMethods–