📜  Java中的字段 isSynthetic() 方法及示例

📅  最后修改于: 2022-05-13 01:54:32.291000             🧑  作者: Mango

Java中的字段 isSynthetic() 方法及示例

Java.lang.reflect.FieldisSynthetic()方法用于检查 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());
        }
    }
}
输出:

参考资料: https: Java Java