class类isSynthetic
public boolean isSynthetic()如果此类是复合类,则返回 true,否则 false。请教:什么是复合类?最好有个例子可以让isSynthetic()返回true的。我在网上查了半天没查到。谢谢!!

解决方案 »

  1.   

    嗯,这个题目很有意思。
    在OO思想中,有聚合与复合的概念,但不敢肯定是这个。
    百度搜 复合类 没有任何结果。
    在google中,输入Synthetic Class,有以下的结果
    According to the JVM Spec: "A class member that does not appear in the source code must be ed using a Synthetic attribute." Also, "The Synthetic attribute was introduced in JDK release 1.1 to support nested classes and interfaces." I know that nested classes are sometimes implemented using synthetic fields and synthetic contructors, e.g. an inner class may use a synthetic field to save a reference to its outer class instance, and it may generate a synthetic contructor to set that field correctly. I'm not sure if it Java still uses synthetic constructors or methods for this, but I'm pretty sure I did see them used in the past. I don't know why they might need synthetic classes here. On the other hand, something like RMI or java.lang.reflect.Proxy should probably create synthetic classes, since those classes don't actually appear in source code. I just ran a test where Proxy did not create a synthetic instance, but I believe that's probably a bug.Hmm, we discussed this some time ago back here. It seems like Sun is just ignoring this synthetic attribute, for classes at least, and we should too.注意上文的第一处黑体部分,一个类的复合属性表示他支持嵌套的类或者接口
    注意上文的第二处黑体部分,说明符合这个概念就是OO思想中的类的复合,也就是只要含有其它类的引用即为复合。概念就是这样了
      

  2.   

    public class type {
    public static void main(String []args){
    if(Boolean.TYPE.getClass().isAnnotation()){
    System.out.println("综合类");
    }
    else
    {
    System.out.println("原始类");
    }
    if(Byte.TYPE.getClass().isAnnotation()){
    System.out.print("综合类");
    }
    else
    {
    System.out.println("原始类");
    }
    if(Character.TYPE.getClass().isAnnotation()){
    System.out.println("综合类");
    }
    else
    {
    System.out.println("原始类");
    }
    if(Short.TYPE.getClass().isAnnotation()){
    System.out.println("综合类");
    }
    else
    {
    System.out.println("原始类");
    }
    if(Integer.TYPE.getClass().isAnnotation()){
    System.out.println("综合类");
    }
    else
    {
    System.out.println("原始类");
    }
    if(Long.TYPE.getClass().isAnnotation()){
    System.out.println("综合类");
    }
    else
    {
    System.out.println("原始类");
    }
    if(Float.TYPE.getClass().isAnnotation()){
    System.out.println("综合类");
    }
    else
    {
    System.out.println("原始类");
    }
    if(Double.TYPE.getClass().isAnnotation()){
    System.out.println("综合类");
    }
    else
    {
    System.out.println("原始类");
    }
    if(Void.TYPE.getClass().isAnnotation()){
    System.out.println("综合类");
    }
    else
    {
    System.out.println("原始类");
    }
    }

    }除了这九个其他都是综合类
    isPrimitive
    public boolean isPrimitive()
    Determines if the specified Class object represents a primitive type. There are nine predefined Class objects to represent the eight primitive types and void. These are created by the Java Virtual Machine, and have the same names as the primitive types that they represent, namely boolean, byte, char, short, int, long, float, and double. These objects may only be accessed via the following public static final variables, and are the only Class objects for which this method returns true. Returns:
    true if and only if this class represents a primitive type
      

  3.   

     isPrimitive()和isAnnotation()相对,知道什么是primitive class, Annotation class就清楚了。