Java Modifier isProtected(mod)方法及实例

来源:这里教程网 时间:2026-02-17 20:58:22 作者:

Java Modifier isProtected(mod)方法及实例

java.lang.reflect.ModifierisProtected(mod) 方法用于检查整数参数是否包括受保护的修改器。如果这个整数参数代表受保护的修改器类型,那么该方法返回true,否则返回false。

语法

public static boolean isProtected(int mod)

参数: 该方法接受一个整数名称,因为mod代表一组修改器。

返回 :如果修改器包括受保护的修改器,该方法返回真,否则返回假。

下面的程序说明了isProtected()方法:
程序1 :

// Java program to illustrate isProtected() method  import java.lang.reflect.*;  public class GFG {      // create a String Field    protected String string;      public static void main(String[] args)        throws NoSuchFieldException,               SecurityException    {          // get Field class object        Field field            = GFG.class                  .getDeclaredField("string");          // get Modifier Integer value        int mod = field.getModifiers();          // check Modifier is protected or not        boolean result = Modifier.isProtected(mod);          System.out.println("Mod integer value "                           + mod + " is protected : "                           + result);    }}

输出。

Mod integer value 4 is protected : true

程序2

// Java program to illustrate isProtected()  import java.lang.reflect.*;  public class GFG {      // create an int Field    public int numbers;      public static void main(String[] args)        throws NoSuchFieldException,               SecurityException    {          // get Field class object        Field field            = GFG.class.getDeclaredField("numbers");          // get Modifier Integer value        int mod = field.getModifiers();          // check Modifier is protected or not        boolean result            = Modifier.isProtected(mod);          System.out.println("Mod integer value "                           + mod + " is protected : "                           + result);    }}

输出。

Mod integer value 1 is protected : false

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

相关推荐