當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Field isSynthetic()用法及代碼示例


java.lang.reflect.Field的isSynthetic()方法用於檢查Field Object是否為合成字段。如果該字段是合成字段,則該函數返回true,否則它將返回false。

合成構造:合成構造是Java編譯器為內部目的而創建的類,字段和方法。

用法:


public boolean isSynthetic()

參數:此方法接受不接受任何內容。

返回:僅當此字段是Java語言規範定義的綜合字段時,此方法才返回true。

以下示例程序旨在說明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()); 
        } 
    } 
}
輸出:

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



相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 Field isSynthetic() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。