我正在读一本书,作者使用了这样的代码
public class Pool<T> {
public interface PoolObjectFactory<T> {
public T createObject();
}
private final List<T> freeObjects;
private final PoolObjectFactory<T> factory;
private final int maxSize;
public Pool(PoolObjectFactory<T> factory, int maxSize) {
this.factory = factory;
this.maxSize = maxSize;
this.freeObjects = new ArrayList<T>(maxSize);
} //end of constructor
} //end of class Pool<T>
然后他使用了类似这样的代码
PoolObjectFactory<KeyEvent> factory = new PoolObjectFactory<KeyEvent>() {
@Override
public KeyEvent createObject() {
return new KeyEvent();
} //end of createObject()
};
keyEventPool = new Pool<KeyEvent>(factory, 100);
我想在
PoolObjectFactory<KeyEvent> factory = new PoolObjectFactory<KeyEvent>() {..};
行中问他没有说implements PoolObjectFactory
。为什么?当您使用界面时,您是否使用implements
关键字?谢谢
最佳答案
您正在此处创建一个匿名类。无需Implements关键字。请注意,在这种情况下,您使用新的PoolObjectFactory,并且可以清楚地指示您正在实现的类-无需再次使用实现来指示它。
另一方面,如果要创建子类,则需要声明其父接口,然后需要Implements关键字。