Java中的字段 isSynthetic() 方法及示例
Java.lang.reflect.Field的isSynthetic()方法用于检查 Field Object 是否为合成字段。如果该字段是合成字段,则该函数返回 true,否则将返回 false。
合成构造:合成构造是Java编译器为内部目的而创建的类、字段和方法。
句法:
public boolean isSynthetic()
参数:此方法不接受任何内容。
Return :当且仅当此字段是Java语言规范定义的合成字段时,此方法才返回true 。
下面的程序说明了 isSynthetic() 方法:
方案一:
// Java program to illustrate isSynthetic() method
import java.lang.reflect.Field;
import java.time.Month;
public class GFG {
public static void main(String[] args)
throws Exception
{
// Get field object
Field field
= Numbers.class.getField("value");
// check field is synthetic or not
System.out.println(
"The Field is isSynthetic: "
+ field.isSynthetic());
}
}
// sample Numbers class
class Numbers {
// static short value
public static long value = 3114256;
}
The Field is isSynthetic: false
方案二:
// Java program to illustrate isSynthetic() method
import java.lang.reflect.Field;
import java.time.DayOfWeek;
public class GFG {
public static void main(String[] args)
throws Exception
{
// Get field object of Month class
Field[] fields
= DayOfWeek.class
.getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
// print name of Fields
System.out.println(
"The Field "
+ fields[i].toString()
+ "\n is isSynthetic:"
+ fields[i].isSynthetic());
}
}
}
The Field public static final java.time.DayOfWeek java.time.DayOfWeek.MONDAY
is isSynthetic:false
The Field public static final java.time.DayOfWeek java.time.DayOfWeek.TUESDAY
is isSynthetic:false
The Field public static final java.time.DayOfWeek java.time.DayOfWeek.WEDNESDAY
is isSynthetic:false
The Field public static final java.time.DayOfWeek java.time.DayOfWeek.THURSDAY
is isSynthetic:false
The Field public static final java.time.DayOfWeek java.time.DayOfWeek.FRIDAY
is isSynthetic:false
The Field public static final java.time.DayOfWeek java.time.DayOfWeek.SATURDAY
is isSynthetic:false
The Field public static final java.time.DayOfWeek java.time.DayOfWeek.SUNDAY
is isSynthetic:false
The Field private static final java.time.DayOfWeek[] java.time.DayOfWeek.ENUMS
is isSynthetic:false
The Field private static final java.time.DayOfWeek[] java.time.DayOfWeek.$VALUES
is isSynthetic:true
参考资料: https: Java Java