return应该返回的是一个Contents对象的实例,可是你的new调用了构造函数,返回值就不匹配了我想是这样子。

解决方案 »

  1.   

    public class a{
    pulic class Contents{};
        public Contents cont(){
             return new Contents(){
                 private int i=11;
                 public int value(){
                    return i;
                 }
              };
        }
    }
    就可以了
      

  2.   

    可是在Thinking in Java 中讲到匿名的内部类时候都是这样的啊,我这段代码是引用这本书第266页。只要在前面加interface Contents就行了,可能是要先定义接口把
      

  3.   

    晕~。Contents 都没定义。你随便定义个类,或抽象类,或接口 Contents也行啊。匿名类,并不是不用定义,或者不用写名字。
    他们一般被用来继承或实现接口,而不用写新的类名。
      

  4.   

    interface Contents在thinking in java中有定义的anonymous inner class实际是这样的:=========================================
    interface Contents{}public class Parcel6{
        public Contents cont(){
             return new Contents(){
                 private int i=11;
                 public int value(){
                    return i;
                 }
              };
        }
    }
    ================================
    等于是:
    interface Contents{}public class Parcel6{
        public class XXXXXX implements Contents{
                 private int i=11;
                 public int value(){
                    return i;
                 }    }
        public Contents cont(){
             return new XXXXXX();
        }
    }
    ====================================
    只是一个有名一个无名