首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > Java > 跟抽象类有关的问题
【标  题】:跟抽象类有关的问题
【关键字】:
【来  源】:http://blog.csdn.net/zdsdiablo/archive/2007/04/11/1560820.aspx

跟抽象类有关的问题

问题描述


首先写了一个抽象类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,需要转换一下

也许会有别的问题,请各位不吝赐教
Tomcat 5.X 常规使用:【上一篇】
Compiling with the Java Compiler API:【下一篇】
【相关文章】
没有相关文章
【随机文章】
  • PHP+MYSQL留言本
  • 巧用Windows 2000快捷方式
  • 终于解决了Xterm中的粘贴问题
  • 以太网技术大全(4)
  • Redhat 7.3汉化/美化补充
  • 什么是面向服务体系结构
  • ADO.NET深入研究(2)
  • 华为软件编码规范.doc
  • mc是一个变暗的亮点,位置就是鼠标的
  • 网络游戏外挂编写基础①
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 软讯网络 All Rigths Reserved.