大神求解java List的一个问题!!谢谢

傻傻的笑TzT 2016-08-24 03:10:23
下面B是A的子类
List<? extends A> result =new ArrayList<B>();
然后调用方法method(result,result);为什么还会报错???
但调用method(result);和method3(result,result)不会报错,是因为参数只能是一个<T>类型的吗?

public <T> boolean method(List<T> list,List<T> list2)throws Exception {
........................
}

public <T> boolean method2(List<T> list)throws Exception {
........................
}

public <T> boolean method3(List<T> list,List<?> list2)throws Exception {
........................
}
...全文
376 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Fly_fans 2016-08-26
  • 打赏
  • 举报
回复
你这个 List<? extends A> result 定义的有问题,可能是java内部的一个检查机制吧。 既然你后面是 new ArrayList<B>(); 那么为什么List后面不是B类型呢 具体原因我也说不上来,还是遵照一下java内部的检测机制吧。
wh180153 2016-08-25
  • 打赏
  • 举报
回复
赞!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
傻傻的笑TzT 2016-08-25
  • 打赏
  • 举报
回复
引用 4 楼 wh180153 的回复:
赞!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
傻傻的笑TzT 2016-08-24
  • 打赏
  • 举报
回复
引用 1 楼 YidingHe 的回复:
楼主要学会贴代码
public class Demo {
    public static void main(String[] args) {
        if (args.length == 0) {
            System.out.println("no args.");
        }
    }
}
像楼主的代码层级多,又没有排版,没法看。
代码比较长,中间省略很多,表述的也不多吧, List<? extends A> r=new ArrayList<B>(); //B继承A method(r,r)报错; method2(r); 和method3(r,r)对的,为什么method(r,r)报错?
傻傻的笑TzT 2016-08-24
  • 打赏
  • 举报
回复
代码比较长,中间省略很多,表述的也不多吧, List<? extends A> r=new ArrayList<B>(); //B继承A method(r,r)报错; method2(r); 和method3(r,r)对的,为什么method(r,r)报错?
捏造的信仰 2016-08-24
  • 打赏
  • 举报
回复
楼主要学会贴代码
public class Demo {
    public static void main(String[] args) {
        if (args.length == 0) {
            System.out.println("no args.");
        }
    }
}
像楼主的代码层级多,又没有排版,没法看。

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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