日期:2014-05-19  浏览次数:20636 次

返回的值是接口的实现为什么会通不过?
返回的值是接口的实现为什么会通不过?

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>

------解决方案--------------------
cannot convert from ArrayList<ABCImpl> to List<IABC> 这还不明显吗?
------解决方案--------------------
ArrayList<ABCImpl> al=new ArrayList<ABCImpl>();
改成
ArrayList<IABC> al=new ArrayList<IABC>();
------解决方案--------------------

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



改为这样就OK了。
------解决方案--------------------
引用:
ArrayList<ABCImpl> al=new ArrayList<ABCImpl>();
改成
ArrayList<IABC> al=new ArrayList<IABC>();
+10086
List<IABC> al=new ArrayList<ABCImpl>();试试
------解决方案--------------------
public static List<IABC> test(){
List<IABC> al=new ArrayList<IABC>();
return al;
}

ArrayList<IABC> 和ArrayList<IABCImpl>不是同一个类型,也不存在任何继承关系
IABC 和 IABCImpl才是继承关系
声明为ArrayList<IABC>可以往里面加入IABCImpl元素。。



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


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

public static List<IABC> test2(){
ArrayList<IABC> al=new ……


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

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

------解决方案--------------------
引用:
难道就是实例化一个list接口的意思?
那么这是最明了的了。
Java code?123 public List<IABC> test(){        return new ArrayList<ICustomerService>(); }

笔误,ICustomerService是我测试的:

?public?List<IABC>?test(){
????????return?new?ArrayList<IABC>();
?}