如果此構造方法對象是綜合構造,則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()
相關用法
- Java Field isSynthetic()用法及代碼示例
- Java Class isSynthetic()用法及代碼示例
- Java Constructor getAnnotation()用法及代碼示例
- Java Constructor getParameterCount()用法及代碼示例
- Java Constructor hashCode()用法及代碼示例
- Java Constructor isVarArgs()用法及代碼示例
- Java Constructor equals()用法及代碼示例
- Java Constructor getParameterAnnotations()用法及代碼示例
- Java Constructor getName()用法及代碼示例
- Java Constructor getGenericExceptionTypes()用法及代碼示例
- Java Constructor getDeclaringClass()用法及代碼示例
- Java Constructor getDeclaredAnnotations()用法及代碼示例
- Java Constructor getGenericParameterTypes()用法及代碼示例
- Java Constructor getAnnotatedReceiverType()用法及代碼示例
- Java Constructor toString()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 Constructor isSynthetic() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。