返回的值是接口的实现为什么会通不过? public List<IABC> test(){
ArrayList<ABCImpl> al=new ArrayList<ABCImpl>();
return al;
} 返回值al不通过,方法的返回类型List<InterFaceABC>,我返回的正是实现list的arraylist,接口IABC的ABCImpl怎么会通不过。提示说:Type mismatch: cannot convert from ArrayList<ABCImpl> to List<IABC>

解决方案 »

  1.   

    cannot convert from ArrayList<ABCImpl> to List<IABC> 这还不明显吗?
      

  2.   

    ArrayList<ABCImpl> al=new ArrayList<ABCImpl>();
    改成
    ArrayList<IABC> al=new ArrayList<IABC>();
      

  3.   


    public List<IABC> test(){
      ArrayList<IABC> al=new ArrayList<ABCImpl>();
      return al;
    }改为这样就OK了。
      

  4.   

    +10086
    List<IABC> al=new ArrayList<ABCImpl>();试试
      

  5.   

    改这样还是同不过,你们按这个写个测试代码都能通过?
    public static List<IABC> test(){
    ArrayList<IABC> al=new ArrayList<ABCImpl>();
    return al;
    }Type mismatch: cannot convert from ArrayList<ABCImpl> to ArrayList<IABC>
      

  6.   

    public static List<IABC> test(){
    List<IABC> al=new ArrayList<IABC>();
    return al;
    }ArrayList<IABC> 和ArrayList<IABCImpl>不是同一个类型,也不存在任何继承关系
    IABC 和 IABCImpl才是继承关系
    声明为ArrayList<IABC>可以往里面加入IABCImpl元素。。
      

  7.   

    public List<IABC> test(){
      List<IABC> al=new ArrayList<IABC>();
      IABC iabc = new ABCImpl();
      al.add(iabc);
      return al;
    }
      

  8.   

    还是我来写两个能通过,不要轻视小问题啊,各位小兄弟们。
            public static List<IABC> test(){
    List<IABC> al=new ArrayList<ABCImpl>();
    return al;
    }

    public static List<IABC> test2(){
    ArrayList<IABC> al=new ArrayList<ABCImpl>();
    return al;
    }
      

  9.   


    你当真你的俩个方法都没有编译出错?
    什么ide?
      

  10.   


    难道就是实例化一个list接口的意思?
    那么这是最明了的了。
     public List<IABC> test(){
         return new ArrayList<ICustomerService>();
     }
      

  11.   

    笔误,ICustomerService是我测试的: public List<IABC> test(){
            return new ArrayList<IABC>();
     }
      

  12.   


    这还不明白啊,就是返回类型是接口泛型,那么你在返回的时候可以返回接口或者是实现类,但是接口指定的泛型类型不能不同,类型是接口那你就得返回接口类型,而不能把接口类型参数用实例类来替换,如改成ArrayList<ABCImpl>或者List<ABCImpl>
      

  13.   

    我上面写的返回类型是能通过,再修改下赋值的就可以了,同理不同参数类型不同也不能new
    public static List<IABC> test(){
    //List<IABC> al=new ArrayList<ABCImpl>();
    List<IABC> al=new ArrayList<IABC>();
    return al;
    }public static List<IABC> test2(){
    //ArrayList<IABC> al=new ArrayList<ABCImpl>();
    ArrayList<IABC> al=new ArrayList<IABC>();
    return al;
    }
    快乐的时光总是短暂的又到了说拜拜!
    今天的课就讲到这下课。