我想问的是在框架,比如定义ArrayList<T>的时候  T能是接口或抽象类吗?然后实例化的时候塞进去的是实现了T接口或者是继承了T的类  这样行吗?我做了一下实验好像不行,不知道怎么改

解决方案 »

  1.   

    可以的,charSequence是接口,我声明了一个放charSequence的arrayList,然后add的是String没有问题呀。
            List<CharSequence> list = new ArrayList<CharSequence>();
            list.add("abc");
            list.add("abc");
    应该是其他问题
      

  2.   

    interface Inter{
    void fun();
    }
    public class Ssh implements Inter {

    @Override
    public void fun() {
    // TODO Auto-generated method stub
    System.out.println("fun()...");
    }
    public static void main(String[] args) throws Exception{

    ArrayList<Inter> al = new ArrayList<Inter> ();
    al.add(new Ssh());

    for(Iterator<Inter> it = al.iterator(); it.hasNext();){
    it.next().fun();
    }
            }
    }