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

JavaNet的牛肉粉丝 2013-01-18 10:04:33
返回的值是接口的实现为什么会通不过?

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>
...全文
240 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
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; } 快乐的时光总是短暂的又到了说拜拜! 今天的课就讲到这下课。
  • 打赏
  • 举报
回复
引用 8 楼 abcmsnet 的回复:
还是我来写两个能通过,不要轻视小问题啊,各位小兄弟们。 public static List<IABC> test(){ List<IABC> al=new ArrayList<ABCImpl>(); return al; } public static List<IABC> test2(){ ArrayList<IABC> al=new ……
这还不明白啊,就是返回类型是接口泛型,那么你在返回的时候可以返回接口或者是实现类,但是接口指定的泛型类型不能不同,类型是接口那你就得返回接口类型,而不能把接口类型参数用实例类来替换,如改成ArrayList<ABCImpl>或者List<ABCImpl>
  • 打赏
  • 举报
回复
引用 10 楼 sd4000784 的回复:
难道就是实例化一个list接口的意思? 那么这是最明了的了。 Java code?123 public List<IABC> test(){ return new ArrayList<ICustomerService>(); }
笔误,ICustomerService是我测试的:
 public List<IABC> test(){
        return new ArrayList<IABC>();
 }
  • 打赏
  • 举报
回复
难道就是实例化一个list接口的意思? 那么这是最明了的了。
 public List<IABC> test(){
    	return new ArrayList<ICustomerService>();
 }
  • 打赏
  • 举报
回复
引用 8 楼 abcmsnet 的回复:
还是我来写两个能通过,不要轻视小问题啊,各位小兄弟们。 public static List<IABC> test(){ List<IABC> al=new ArrayList<ABCImpl>(); return al; } public static List<IABC> test2(){ ArrayList<IABC> al=new ……
你当真你的俩个方法都没有编译出错? 什么ide?
  • 打赏
  • 举报
回复
还是我来写两个能通过,不要轻视小问题啊,各位小兄弟们。 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; }
  • 打赏
  • 举报
回复
public List<IABC> test(){
  List<IABC> al=new ArrayList<IABC>();
  IABC iabc = new ABCImpl();
  al.add(iabc);
  return al;
}
绝情小舞 2013-01-18
  • 打赏
  • 举报
回复
public static List<IABC> test(){ List<IABC> al=new ArrayList<IABC>(); return al; } ArrayList<IABC> 和ArrayList<IABCImpl>不是同一个类型,也不存在任何继承关系 IABC 和 IABCImpl才是继承关系 声明为ArrayList<IABC>可以往里面加入IABCImpl元素。。
  • 打赏
  • 举报
回复
改这样还是同不过,你们按这个写个测试代码都能通过? public static List<IABC> test(){ ArrayList<IABC> al=new ArrayList<ABCImpl>(); return al; } Type mismatch: cannot convert from ArrayList<ABCImpl> to ArrayList<IABC>
  • 打赏
  • 举报
回复
引用 2 楼 AA5279AA 的回复:
ArrayList<ABCImpl> al=new ArrayList<ABCImpl>(); 改成 ArrayList<IABC> al=new ArrayList<IABC>();
+10086 List<IABC> al=new ArrayList<ABCImpl>();试试
  • 打赏
  • 举报
回复

public List<IABC> test(){
  ArrayList<IABC> al=new ArrayList<ABCImpl>();
  return al;
}
改为这样就OK了。
失落夏天 2013-01-18
  • 打赏
  • 举报
回复
ArrayList<ABCImpl> al=new ArrayList<ABCImpl>(); 改成 ArrayList<IABC> al=new ArrayList<IABC>();
  • 打赏
  • 举报
回复
cannot convert from ArrayList<ABCImpl> to List<IABC> 这还不明显吗?

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧