Java和泛型 [英] Java and generics

查看:126
本文介绍了Java和泛型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对java很陌生,所以我很抱歉,如果我完全错误的结束了。



我想写一个通用的(在英文意义上的单词!)数据访问类。
例如我现在有:

  public class DA< T> {
public static Dao getAccountDao()throws NamingException,SQLException {
Context ctx = new InitialContext();
DataSource dataSource =(DataSource)ctx.lookup(java:comp / env / jdbc / test);
ConnectionSource connectionSource = new DataSourceConnectionSource(dataSource,new MysqlDatabaseType());
Dao< Account,Integer> accountDao = DaoManager.createDao(connectionSource,Account.class);
返回accountDao;
}
}

我可以这样调用它:

  Dao< Account,Integer> accountDao = DA.getAccountDao(); 

但是每个Dao /模型都需要一个版本。所以我试图做一些可以这样调用的东西:

  Dao< SomeClass,Integer> someClassDao = DA.getDao(SomeClass); 

这甚至可能吗?

I已经尝试过这样的事情:

  public class DA {
public static Dao getDao(< T>)throws NamingException ,SQLException {
Context ctx = new InitialContext();
DataSource dataSource =(DataSource)ctx.lookup(java:comp / env / jdbc / test);
ConnectionSource connectionSource = new DataSourceConnectionSource(dataSource,new MysqlDatabaseType());
Dao 返回accountDao;
}

}

但Netbeans给出了错误:类型非法开始



我的大脑在仿制药方面挣扎,这是他们可以做
$ b

编辑:从下面的帖子的帮助我必须:

 

public class DA< T> {
public static Dao< T,Integer> getDao(T daoType)抛出NamingException,SQLException {
Dao< T,Integer> accountDao = DaoManager.createDao(T.class);
返回accountDao;
}

}

这会产生两个错误:
非静态类型变量T不能从静态上下文中引用
,如果我删除静态关键字,我得到:
不能从类型变量中选择
我需要阅读泛型和静态一起工作,但第二个看起来像擦除的结果(http://www.coderanch.com/t/386358/java/java/Converting-type-parameters-class),所以不知道它是否可能。



前面提到过,Dao的东西使用了一个名为ORMLite的ORM库,所以createDao等不是我的代码。

T.class 来访问你的意思,你必须将类对象传入方法:

  class Account {} 

class Dao< TEntity,TId> {}

class DA {
//您的DaoManager.createDao()也需要类似的签名
public static< TEntity> Dao< TEntity,Integer> getDao(Class< TEntity> daoType){
/// yadda blah,根据需要创建DAO
返回新的Dao< TEntity,Integer>();



public class Test {
public static void main(String [] args){
//将类文字传递给你要将TEntity作为参数
Dao< Account,Integer> dao = DA.getDao(Account.class);
}
}


I'm new to java so apologies if I've got totally the wrong end of the stick.

I'm trying to write a generic (in the English sense of the word!) Data Access class. eg I currently have:

public class DA<T> {
public static Dao getAccountDao() throws NamingException, SQLException {
    Context ctx = new InitialContext();
    DataSource dataSource = (DataSource)ctx.lookup("java:comp/env/jdbc/test");
    ConnectionSource connectionSource = new DataSourceConnectionSource(dataSource, new MysqlDatabaseType());            
    Dao<Account, Integer> accountDao =  DaoManager.createDao(connectionSource, Account.class);
    return accountDao;
}
}

And I can call this with:

Dao<Account, Integer> accountDao = DA.getAccountDao();

But I'll need a version of this for every Dao/model. So I'm trying to make something that can be called like:

Dao<SomeClass, Integer> someClassDao = DA.getDao(SomeClass);

Is this even possible?

I've tried things like:

public class DA {
public static Dao getDao(<T>) throws NamingException, SQLException {
    Context ctx = new InitialContext();
    DataSource dataSource = (DataSource)ctx.lookup("java:comp/env/jdbc/test");
    ConnectionSource connectionSource = new DataSourceConnectionSource(dataSource, new MysqlDatabaseType());            
    Dao<T, Integer> accountDao =  DaoManager.createDao(connectionSource, T.class);
    return accountDao;
}

}

but Netbeans gives the error: illegal start of type

My brain is struggling with generics, is this something they can do?!

EDIT: With help from the posts below I've got to:

public class DA<T> {
public static Dao<T, Integer> getDao(T daoType) throws NamingException, SQLException {
    Dao<T, Integer> accountDao =  DaoManager.createDao(T.class);
    return accountDao;
}

}

Which generates two errors: non-static type variable T cannot be referenced from a static context and if I remove the static keyword, I get: cannot select from a type variable I need to read up on how generics and static work together, but the 2nd looks like a consequence of erasure (http://www.coderanch.com/t/386358/java/java/Converting-type-parameters-class) , so not sure if it's going to be possible.

Should have mentioned earlier, the Dao stuff is using an ORM library called ORMLite, so createDao etc isn't my code.

解决方案

To access what you mean by T.class, you'll have to pass the class object into the method:

class Account {}

class Dao<TEntity, TId> {}

class DA {
    // your DaoManager.createDao() will also need a similar signature
    public static <TEntity> Dao<TEntity, Integer> getDao(Class<TEntity> daoType) {
        /// yadda blah, create DAO as appropriate
        return new Dao<TEntity, Integer>();
    }
}

public class Test {
    public static void main(String[] args) {
        // Pass the class literal for what you want TEntity to be as a parameter
        Dao<Account, Integer> dao = DA.getDao(Account.class);
    }
}

这篇关于Java和泛型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆