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–
相關用法
- Java Field get()用法及代碼示例
- Java Field set()用法及代碼示例
- Java Field getName()用法及代碼示例
- Java Field isSynthetic()用法及代碼示例
- Java Field getShort()用法及代碼示例
- Java Field toGenericString()用法及代碼示例
- Java Field getInt()用法及代碼示例
- Java Field getLong()用法及代碼示例
- Java Field getAnnotatedType()用法及代碼示例
- Java Field setShort()用法及代碼示例
- Java Field setDouble()用法及代碼示例
- Java Field setChar()用法及代碼示例
- Java Field setFloat()用法及代碼示例
- Java Field setInt()用法及代碼示例
- Java Field setByte()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 Field isEnumConstant() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。