getmethods_Java类类getMethods()方法及示例

getmethods

类的类getMethods()方法 (Class class getMethods() method)

  • getMethods() method is available in java.lang package.

    getMethods()方法在java.lang包中可用。

  • getMethods() method is used to return an array of Method objects that indicate all the public methods of the class or interfaces along with it includes all inherited public methods from parent classes or parent interfaces.

    getMethods()方法用于返回一个Method对象数组,这些对象指示该类或接口的所有公共方法,以及它包括从父类或父接口继承的所有公共方法。

  • getMethods() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.

    getMethods()方法是一个非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。

  • getMethods() method may throw an exception at the time of returning an array of Method objects.

    getMethods()方法在返回Method对象数组时可能会引发异常。

    SecurityException: In this exception, it may raise when the security manager exists.

    SecurityException :在此异常中,当安全管理器存在时可能会引发此异常。

Syntax:

句法:

    public Method[] getMethods();

Parameter(s):

参数:

  • It does not accept any parameter.

    它不接受任何参数。

Return value:

返回值:

The return type of this method is Method[], it returns an array of Method object denoting all the public method of this class including all public inherited Methods represented by this Class object.

此方法的返回类型为Method [] ,它返回一个Method对象数组,该数组表示此类的所有公共方法,包括由此Class对象表示的所有公共继承的Method。

Note:

注意:

  • When class or interface does not contain public method, 0 is returned.

    当类或接口不包含公共方法时,将返回0。

  • When this class object holds primitive, an array class, or void type, 0 is returned.

    当此类对象包含基本类型,数组类或void类型时,将返回0。

Example:

例:

// Java program to demonstrate the example 
// of Method[] getMethods () 
// method of Class

import java.lang.reflect.*;

public class GetMethodsOfClass {
    public static void main(String[] args) throws Exception {
        Thread th = new Thread();

        // Get Class object of String
        Class cl = th.getClass();

        // Calling Thread Class Methods
        Method[] methods = cl.getMethods();

        for (int i = 0; i < methods.length; ++i) {
            System.out.print("String Class Methods = ");
            System.out.println(methods[i].toString());
        }
    }
}

Output

输出量

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()


翻译自: https://www.includehelp.com/java/class-class-getmethods-method-with-example.aspx

getmethods

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Java反射可以获取一个类中的所有方法。实现思路如下: 1. 使用`Class.forName()`方法获取指定的Class对象。 2. 使用`getMethods()`或`getDeclaredMethods()`方法获取类中的所有方法。 3. 可以通过遍历获取到的方法数组来获取每个方法的详细信息。 下面是一个使用Java反射获取类中所有方法示例代码: ```java import java.lang.reflect.Method; public class ReflectionExample { public static void main(String\[\] args) throws ClassNotFoundException { Class<?> clazz = Class.forName("com.example.MyClass"); // 替换为你要获取方法的类的完整路径 Method\[\] methods = clazz.getDeclaredMethods(); for (Method method : methods) { System.out.println(method.getName()); } } } class MyClass { public void method1() { // 方法1的实现 } private void method2() { // 方法2的实现 } protected void method3() { // 方法3的实现 } } ``` 在上述示例中,我们使用`Class.forName()`方法获取了指定类的Class对象,然后使用`getDeclaredMethods()`方法获取了该类中的所有方法。最后,我们通过遍历方法数组,使用`getName()`方法获取每个方法的名称并打印出来。 请注意,上述示例中的类名和方法名仅作为示例,你需要根据你的实际情况替换为你要获取方法的类的完整路径和方法名。 #### 引用[.reference_title] - *1* [如何使用Java反射机制获取一个类中全部方法呢?](https://blog.csdn.net/qq_25073223/article/details/126136202)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [Java反射调用类的私有方法和内部类的私有方法](https://blog.csdn.net/weixin_45644323/article/details/120753661)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值