当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。