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


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


如果此構造方法對象是綜合構造,則java.lang.reflect.Constructor類的isSynthetic()方法用於返回布爾值true,否則,方法返回false表示此構造不是綜合構造。眾所周知,合成構造是Java編譯器出於內部目的而創建的類,字段和方法。對於構造器,合成構造是由Java編譯器為內部使用而創建的構造器。此方法有助於識別這些構造。

用法:

public boolean isSynthetic()

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


返回:僅當此可執行文件是Java語言規範所定義的綜合結構時,此方法才返回true。

以下示例程序旨在說明isSynthetic()方法:
示例1:

// Java program to illustrate isSynthetic() method 
  
import java.lang.reflect.Constructor; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
        // create a class object 
        Class classObj = shape.class; 
  
        // get Constructor object 
        // array from class object 
        Constructor[] cons = classObj.getConstructors(); 
  
        // check isSynthetic or not 
        boolean answer = cons[0].isSynthetic(); 
  
        // print result 
        System.out.println("isSynthetic : " + answer); 
    } 
  
    @CustomAnnotation(createValues = "GFG") 
    public class shape { 
  
        @CustomAnnotation(createValues = "GFG") 
        public shape() 
        { 
        } 
    } 
  
    // create a custom annotation 
    public @interface CustomAnnotation { 
        public String createValues(); 
    } 
    }
輸出:
isSynthetic : false

示例2:

// Java program to illustrate isSynthetic() method 
  
import java.lang.annotation.Annotation; 
import java.lang.reflect.Constructor; 
  
public class GFG { 
  
    public static void main(String[] args) 
    { 
        // create a class object 
        Class classObj = String.class; 
  
        // get Constructor object 
        // array from class object 
        Constructor[] cons = classObj.getConstructors(); 
  
        for (int i = 0; i < cons.length; i++) { 
  
            System.out.println("Constructor: "
                               + cons[i]); 
  
            // get array of isSynthetic 
            boolean answer = cons[0].isSynthetic(); 
  
            // print result 
            System.out.println("isSynthetic : " + answer); 
        } 
    } 
}
輸出:

Constructor: public java.lang.String(byte[], int, int)
isSynthetic : false
Constructor: public java.lang.String(byte[], java.nio.charset.Charset)
isSynthetic : false
Constructor: public java.lang.String(byte[], java.lang.String) throws java.io.UnsupportedEncodingException
isSynthetic : false
Constructor: public java.lang.String(byte[], int, int, java.nio.charset.Charset)
isSynthetic : false
Constructor: public java.lang.String(byte[], int, int, java.lang.String) throws java.io.UnsupportedEncodingException
isSynthetic : false
Constructor: public java.lang.String(java.lang.StringBuilder)
isSynthetic : false
Constructor: public java.lang.String(java.lang.StringBuffer)
isSynthetic : false
Constructor: public java.lang.String(byte[])
isSynthetic : false
Constructor: public java.lang.String(int[], int, int)
isSynthetic : false
Constructor: public java.lang.String()
isSynthetic : false
Constructor: public java.lang.String(char[])
isSynthetic : false
Constructor: public java.lang.String(java.lang.String)
isSynthetic : false
Constructor: public java.lang.String(char[], int, int)
isSynthetic : false
Constructor: public java.lang.String(byte[], int)
isSynthetic : false
Constructor: public java.lang.String(byte[], int, int, int)
isSynthetic : false

參考文獻: https://docs.oracle.com/javase/10/docs/api/java/lang/reflect/Constructor.html#isSynthetic()



相關用法


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