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


Java Field isEnumConstant()用法及代码示例


java.lang.reflect.Field的isEnumConstant()方法,用于检查此字段是否表示枚举类型的元素。如果此字段表示枚举类型的元素,则方法返回true,否则返回false。

用法:

public boolean isEnumConstant()

参数:此方法接受不接受任何内容。


返回:仅当此字段表示枚举类型的元素时,此方法才返回true。

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

// Java program to illustrate 
// isEnumConstant () method 
  
import java.lang.reflect.Field; 
import java.time.Month; 
  
public class GFG { 
  
    public static void main(String[] args) 
        throws NoSuchFieldException 
    { 
  
        // get all declared fields of Month class 
        Field[] declaredFields 
            = Month.class.getDeclaredFields(); 
  
        // loop through Field array 
        // and print Field name and 
        // check whether the field is EnumConstant 
        for (int i = 0; i < declaredFields.length; i++) { 
            System.out.print( 
                "Field --> Name: "
                + declaredFields[i].getName() 
                + "; isEnumConstant: "
                + declaredFields[i] 
                      .isEnumConstant() 
                + "\n"); 
        } 
    } 
}
输出:
Field --> Name: JANUARY; isEnumConstant: true
Field --> Name: FEBRUARY; isEnumConstant: true
Field --> Name: MARCH; isEnumConstant: true
Field --> Name: APRIL; isEnumConstant: true
Field --> Name: MAY; isEnumConstant: true
Field --> Name: JUNE; isEnumConstant: true
Field --> Name: JULY; isEnumConstant: true
Field --> Name: AUGUST; isEnumConstant: true
Field --> Name: SEPTEMBER; isEnumConstant: true
Field --> Name: OCTOBER; isEnumConstant: true
Field --> Name: NOVEMBER; isEnumConstant: true
Field --> Name: DECEMBER; isEnumConstant: true
Field --> Name: ENUMS; isEnumConstant: false
Field --> Name: $VALUES; isEnumConstant: false

示例2:

// Java program to illustrate 
// isEnumConstant () method 
  
import java.lang.reflect.Field; 
  
public class GFG { 
  
    public static void main(String[] args) 
        throws NoSuchFieldException 
    { 
  
        // get all declared fields of Symbols class 
        Field[] declaredFields 
            = Symbols.class.getDeclaredFields(); 
  
        // loop through Field array 
        // and print Field name and 
        // check whether the field is EnumConstant 
        for (int i = 0; i < declaredFields.length; i++) { 
            System.out.print( 
                "Field --> Name: "
                + declaredFields[i].getName() 
                + "; isEnumConstant: "
                + declaredFields[i] 
                      .isEnumConstant() 
                + "\n"); 
        } 
    } 
  
    private static enum Symbols { 
        Alpha, 
        Beta, 
        Gamma 
    } 
}
输出:
Field --> Name: Alpha; isEnumConstant: true
Field --> Name: Beta; isEnumConstant: true
Field --> Name: Gamma; isEnumConstant: true
Field --> Name: ENUM$VALUES; isEnumConstant: false

参考文献: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Field.html#isEnumConstant–



相关用法


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