Java中的修饰符 isNative(mod) 方法及示例
Java.lang.reflect.Modifier的isNative(mod)方法用于检查整数参数是否包含本机修饰符。如果此整数参数表示本机类型修饰符,则方法返回 true,否则返回 false。
句法:
public static boolean isNative(int mod)
参数:此方法接受一个整数名称,因为 mod 表示一组修饰符。
Return :如果 mod 包含 native 修饰符,此方法返回 true,否则返回 false。
下面的程序说明了 isNative() 方法:
方案一:
// 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
方案二:
// 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: Java/lang/reflect/Modifier.html#isNative(int)