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


Java Modifier isNative(mod)用法及代码示例


java.lang.reflect.Modifier的isNative(mod)方法用于检查integer参数是否包含本机修饰符。如果此整数参数表示本机类型Modifier,则方法返回true,否则返回false。

用法:

public static boolean isNative(int mod)

参数:此方法接受整数名称,因为mod代表一组修饰符。


返回:如果mod包含本机修饰符,则此方法返回true,否则返回false。

以下示例程序旨在说明isNative()方法:
示例1:

// Java program to illustrate isNative() method 
  
import java.lang.reflect.Method; 
import java.lang.reflect.Modifier; 
  
public class GFG { 
  
    public static void main(String[] args) 
        throws NoSuchFieldException 
    { 
  
        // get Method class object 
        Method[] methods 
            = Calculator.class.getMethods(); 
  
        // get Modifier Integer value 
        int mod = methods[0].getModifiers(); 
  
        // check Modifier is native or not 
        boolean result = Modifier.isNative(mod); 
  
        System.out.println("Mod integer value "
                           + mod + " is native : "
                           + result); 
    } 
  
    class Calculator { 
        native void addNumbers(); 
    } 
}
输出:
Mod integer value 17 is native : false

示例2:

// Java program to illustrate isNative() 
  
import java.lang.reflect.*; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
  
        // get Method class object 
        Method[] methods 
            = Numbers.class.getMethods(); 
  
        // get Modifier Integer value 
        int mod1 = methods[0].getModifiers(); 
        int mod2 = methods[1].getModifiers(); 
  
        // check Modifiers are native or not 
        boolean result1 = Modifier.isNative(mod1); 
        boolean result2 = Modifier.isNative(mod2); 
  
        // print results 
        System.out.println("Mod integer value "
                           + mod1 + " for method "
                           + methods[0].getName() 
                           + " is native : "
                           + result1); 
  
        System.out.println("Mod integer value "
                           + mod2 + " for method"
                           + methods[1].getName() 
                           + " is native : "
                           + result2); 
    } 
  
    // sample native class 
    abstract class Numbers { 
  
        abstract public int initializeNumber(); 
        int declareNumbers() 
        { 
            return 0; 
        } 
    } 
}
输出:
Mod integer value 1025 for method initializeNumber is native : false
Mod integer value 17 for methodwait is native : false

参考文献: https://docs.oracle.com/javase/10/docs/api/java/lang/reflect/Modifier.html#isNative(int)



相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 Modifier isNative(mod) method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。