我有一个班级,我认为它是单身,抽象和通用的:
public abstract class GenericDaoImpl<E, K extends Serializable> implements GenericDao<E, K> {
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;
private final Class<? extends E> daoType = DaoTypeInit();
private static Class<?> DAO;
public static <T> T getDAO(Class<T> T) throws InstantiationException, IllegalAccessException {
if (DAO == null) {
Class currentClass = new Object() { }.getClass().getEnclosingClass();
DAO = (Class<?>)currentClass.newInstance();
}
return (T) DAO;
}
在扩展GenericDaoImpl的子类中,它似乎找不到getDAO,尽管它显然存在并继承。
StudentDaoImpl studentDAO = StudentDaoImpl.getDAO(Student.class);
我做错了什么,谢谢?
答案 0 :(得分:0)
你的问题是Dao是静态的!
public static <T> T getDAO
应该是:
public <T> T getDAO
除非他们从GenericDaoimpl类中调用它,否则子类无法访问它。
答案 1 :(得分:0)
此代码存在多个问题,但主要问题是静态方法和变量未被继承,new Object() { }.getClass().getEnclosingClass();
将始终返回GenericDaoImpl.class。
如果你想要一个懒惰的初始化单例实例,你应该在StudentDaoImpl上声明它。
答案 2 :(得分:0)
我的代码的当前版本如下:
0001be1731ee7d1c519bc7e87110c9eb880cb396 This is a test
0001bfa0c494c01f9f8c141c476c11bb4625a746 This is a test
0002bd9c3d654698bb514194c4f4171ad6992266 This is a test
000433e0ef411c2cb8ee1727002d6ba15fe9426b This is a test
00051f5350f4d9f3f4f5ba181b0a66d749b161ee This is a test
00066c85bf96469b905e2fb148095448797b2368 This is a test
0007b1a0334de785b3189b67bb73276d602fb7d4 This is a test
0007d018861d588e99e834fc29ca76a523b20e35 This is a test
000992b67ed22d2707ba65046d523ce66dfcfcb8 This is a test
000ad93a0819e2cbd7f0193e1d1ec481a0241b44 This is a test
它编译并运行。任何评论,你能看到这个代码的任何潜在问题吗?
由于