我得到了以下对象:没有构造DbRepository实现在子类
public class DbRepository<T> implements DbRepositoryInterface<T>{
protected T model;
protected DbRepository(T model) {
System.out.println("DbRepository created.");
this.model = model;
}
@Override
public T getModel() {
return model;
}
}
public interface DbRepositoryInterface<T> {
public T getModel();
}
public class PlayerDbRepository extends DbRepository<Player> {
}
在我的代码,在这里我想利用PlayerDbRepository
的,我想打电话:
DbRepositoryInterface<Player> repo = new PlayerDbRepository();
但在PlayerDbRepository
中,我收到了有关我的父类DbRepository
中的构造函数的警告。我需要修改什么来完成这项工作?或者我的方法在这个错误?
您可以通过创建实现接口的类或匿名类来创建接口对象。向工厂添加像工厂这样的额外图层不会导致OP的问题消失,只会将其推向另一层。 – dkatzel 2014-10-07 15:59:51
你是对的。然后,尝试在父类上添加另一个空参数构造函数。 公共DbRepository(){} 应该做的伎俩。 – Jedi 2014-10-08 10:00:11