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


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


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

用法:

public static boolean isProtected(int mod)

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


返回:如果mod包含protected修饰符,则此方法返回true;否则,返回true。否则为假。

以下示例程序旨在说明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)



相关用法


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