當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Method類 getModifiers()用法及代碼示例


Method類的java.lang.reflect.Method.getModifiers()方法返回此Method對象表示的方法的修飾符。它返回int值。然後使用use Modifier類,獲取與該值對應的修飾符的名稱。

用法:

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)以外的方法對象的結果。這些方法是從超類名稱繼承的,按類 Object 。

參考: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Method.html#getModifiers-




相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 Method Class | getModifiers() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。