首页 > 简单的程序,为什么interface可以extends?还有构造函数可以泛型化吗?

简单的程序,为什么interface可以extends?还有构造函数可以泛型化吗?

interface FactoryI<T> {
  T create();
}

class Foo2<T> {
  private T x;
  public <F extends FactoryI<T>> Foo2(F factory) {
    x = factory.create();
  }
  // ...
}

class IntegerFactory implements FactoryI<Integer> {
  public Integer create() {
    return new Integer(0);
  }
}    

class Widget {
  public static class Factory implements FactoryI<Widget> {
    public Widget create() {
      return new Widget();
    }
  }
}

public class FactoryConstraint {
  public static void main(String[] args) {
    new Foo2<Integer>(new IntegerFactory());
    new Foo2<Widget>(new Widget.Factory());
  }
}

接口肯定可以继承的,这个看看资料就可以知道,
至于构造方法,不是构造方法泛型化,就这个类是一个泛型类,怎么说呢,就是自定义一个泛型类就是这样一个格式,也可以看看资料上关于泛型的讲解,还可以看一下List Map等这些泛型类的源码

【热门文章】
【热门文章】