📜  方法类 | Java中的 getModifiers() 方法

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

方法类 | Java中的 getModifiers() 方法

Method 类的Java.lang.reflect.Method.getModifiers()方法返回此 Method 对象表示的方法的修饰符。它返回 int 值。然后使用使用修饰符类,获取对应于该值的修饰符的名称。

句法:

public int getModifiers()

返回值:此方法返回此 Method 对象表示的方法的修饰符

下面的程序说明了 Method 类的 getModifiers() 方法:

程序 1:该程序在对类的方法对象应用 getModifiers() 时打印方法的修饰符。

// Program Demonstrate how to apply getModifiers() method
// of Method Class.
  
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
  
public class GFG {
  
    // Main method
    public static void main(String[] args)
    {
  
        try {
            // create class object
            Class classobj = demo.class;
  
            // get object of setValue() method of demo class
            Method[] methods = classobj.getMethods();
            Method setValueMethodObject = methods[0];
  
            // get modifier of setValueMethodObject
            int modifier = setValueMethodObject.getModifiers();
  
            // print modifier details
            System.out.println("Modifier of setValue():");
            System.out.println(Modifier.toString(modifier));
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
  
    // sample class contains method with public modifier
    class demo {
        // method has public modifier
        public int setValue()
        {
            System.out.println("setValue");
            return 24;
        }
    }
}
输出:
Modifier of setValue():
public

程序2:程序演示了如何应用方法类的getModifiers()方法。程序为 democlass 类的所有方法打印修饰符名称。

// Program Demonstrate how to apply getModifiers() method
// of Method Class.
  
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
  
public class GFG {
  
    // Main method
    public static void main(String[] args)
    {
        try {
            // create class object
            Class classobj = democlass.class;
  
            // get list of methods of democlass
            Method[] methods = classobj.getMethods();
  
            // loop through methods list
            for (Method method : methods) {
  
                // get Modifier of current method
                int modifier = method.getModifiers();
  
                // print method name
                System.out.println("Modifier of method name:" 
+ method.getName());
  
                // print Modifier details
                System.out.println(Modifier.toString(modifier));
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}
// a simple class
class democlass {
  
    // method has static modifier
    public static int method1()
    {
        System.out.println("setValue");
        return 24;
    }
  
    // method has public final modifier
    public final String method2()
    {
        System.out.println("getValue");
        return "getValue";
    }
}
输出:
Modifier of method name:method1
public static
Modifier of method name:method2
public final
Modifier of method name:wait
public final
Modifier of method name:wait
public final native
Modifier of method name:wait
public final
Modifier of method name:equals
public
Modifier of method name:toString
public
Modifier of method name:hashCode
public native
Modifier of method name:getClass
public final native
Modifier of method name:notify
public final native
Modifier of method name:notifyAll
public final native

说明:该程序的输出还显示方法对象的结果,而不是类对象中定义的方法,如wait,equals,toString,hashCode,getClass,notify,notifyAll。这些方法是从Java.lang lang包的超类名称对象中继承的。目的。

参考: https: Java/lang/reflect/Method.html#getModifiers–