Java Field isSynthetic()方法及示例

Java Field isSynthetic()方法及示例

java.lang.reflect.FieldisSynthetic() 方法用于检查Field对象是否是一个合成字段。如果该字段是一个合成字段,则该函数返回真,否则将返回假。 合成结构 :合成结构是由Java编译器为内部目的而创建的类、字段和方法。

语法

public boolean isSynthetic()

参数: 此方法不接受任何东西。
返回 :此方法返回 ,当且仅当此字段是Java语言规范中定义的合成字段。

以下程序说明了isSynthetic()方法:

程序1 :

// 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

程序2

// 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());
        }
    }
}

Output:

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://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Field.html#isSynthetic-java

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程