跟抽象类有关的问题

十分钟年华老去 2007-04-11 03:51:09
首先写了一个抽象类A
然后写了三个子类B、C和D

在写一个方法的时候会传递三个子类的List对象
mothodOne(List<B> blist, List<C> clist, List<D> dlist){
for(B b : blist){
b.mothodA();
}
for(C c : clist){
c.mothodA();
}
for(D d : dlist){
d.mothodA();
}
...
}
在方法内部实现的代码是相同的,于是想用抽象类来实现,就改成了
mothodOne(List<A>... abstractList){
for(List<A> thislist : abstractList){
for(A a : blist){
a.mothodA();
}
}
...
}

很荣幸这个方法编译能通过,可是在实现的时候却失败了
List<B> blist = new ArrayList<B>();
List<C> clist = new ArrayList<C>();
List<D> dlist = new ArrayList<D>();

mothodOne(blist, clist, dlist); //这一句就不能编译通过了, eclipse报“不适用”



冰天雪地求解,最好说的清楚一点,问题出现在哪里,如果这种方案不合理,给个更合理的吧
...全文
247 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复

方法
mothodOne(AbstractList... abstractList){ //修改接口为抽象类
for(List<A> thislist : abstractList){
for(A a : thislist){
a.mothodA();
}
}
...
}

实现
AbstractList<B> blist = new ArrayList<B>(); //修改接口为抽象类
AbstractList<C> clist = new ArrayList<C>();
AbstractList<D> dlist = new ArrayList<D>();

blist= new ArrayList<B>(abc.getBList()); //也许会有getlist,需要转换一下

也许会有别的问题,请各位不吝赐教


如果没有别的回帖,我就结贴了,希望版主不会觉得不爽
  • 打赏
  • 举报
回复
为了减少代码才用的抽象类来搞,你再弄一遍add循环,还不如搞成原来的样子呢。
刚才我已经试过了,我的方法可以用,明天贴过来。也许会有更理想的,期待中...
leojay1 2007-04-11
  • 打赏
  • 举报
回复
数据库读出来,可以循环add 嘛
leojay1 2007-04-11
  • 打赏
  • 举报
回复
mothodOne(List<List> abstractList){
for(List<A> thislist : abstractList){
for(A a : thislist){
a.mothodA();
}
}
----------------------------调用
List<B> blist = new ArrayList<B>();
List<C> clist = new ArrayList<C>();
List<D> dlist = new ArrayList<D>();

List<List> l = new ArrayList<List>();
//这里把上面的东西add进来
l.add(blist );
l.add(clist );
l.add(dlist );
mothodOne(l);
  • 打赏
  • 举报
回复
我从数据库拿到的list不可能一个一个add的,不过我刚刚已经想到了解决办法,楼上可以想想我是怎么做的,呵呵
leojay1 2007-04-11
  • 打赏
  • 举报
回复
上面有点问题
List<List> l = new ArrayList<List>();
//这里把上面的东西add进来
l.add(blist );
l.add(clist );
l.add(dlist );
mothodOne(l);
leojay1 2007-04-11
  • 打赏
  • 举报
回复
mothodOne(List<List> abstractList){
for(List<A> thislist : abstractList){
for(A a : thislist){
a.mothodA();
}
}
----------------------------调用
List<B> blist = new ArrayList<B>();
List<C> clist = new ArrayList<C>();
List<D> dlist = new ArrayList<D>();

List<List> l = new ArrayList<List>();
mothodOne(l);
wwwasdf5 2007-04-11
  • 打赏
  • 举报
回复

给大家介绍个技术群

大家一起学习,一起提高

32517633
  • 打赏
  • 举报
回复
在方法内部实现的代码是相同的,于是想用抽象类来实现,就改成了
mothodOne(List<A>... abstractList){
for(List<A> thislist : abstractList){
for(A a : thislist){ //有一块写错了,抱歉
a.mothodA();
}
}
...
}

62,614

社区成员

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

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