问题描述
首先写了一个抽象类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 : thislist){
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报“不适用”
解决方案
方法
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,需要转换一下
也许会有别的问题,请各位不吝赐教
首先写了一个抽象类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 : thislist){
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报“不适用”
解决方案
方法
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,需要转换一下
也许会有别的问题,请各位不吝赐教