当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。