我如何传递一个类作为参数,并返回一个通用集合在Java? [英] How can I pass a Class as parameter and return a generic collection in Java?

查看:131
本文介绍了我如何传递一个类作为参数,并返回一个通用集合在Java?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的Java应用程序设计了一个简单的数据访问对象。我有几个类(记录),表示 User Fruit 表中的单个行。



我想有一个方法来获取特定类型的所有记录。



  public List< User> getAllUsers(){
...
}

public List< Fruit> getAllFruits(){
...
}

....

但我想有一个单一的多态性方法像这样(错):

  public List< T> getAllRecords(Class< T> type){
if(type instanceof User){
//使用JDBC和SQL SELECT * FROM user
} else if(type instanceof Fruit){
//使用JDBC和SQL SELECT * FROM fruit
}
return collection;
}

使用示例:

 列表< Fruit> fruits = myDataAccessObject.getAllRecrods(Fruit.class); 
List< User> users = myDataAccessObject.getAllRecords(User.class);

如何在Java中执行此操作?

解决方案

因为你说你不想在不同的类中的数据访问方法(在anish的回答的评论),我想为什么不尝试这样的东西。

  public class Records {

public interface RecordFetcher< T> {
public List< T> getRecords();
}
static RecordFetcher< Fruit> Fruit = new RecordFetcher< Fruit>(){
public List< Fruit> getRecords(){
...
}
};


static RecordFetcher< User> User = new RecordFetcher< User>(){
public List< User> getRecords(){
...
}
};

public static void main(String [] args){
List< Fruit> fruitRecords = Records.Fruit.getRecords();
List< User> userRecords = Records.User.getRecords();

}
}

strong>



我想再添加一个实现。

  public class Test 
{
public static void main(String [] args)
{
test dataAccess = new Test();
List< Fruit> FruitList = dataAccess.getAllRecords(Fruit.myType);
List< User> UserList = dataAccess.getAllRecords(User.myType);
}
< T>列表< T> getAllRecords(Tcl)
{
List< T> list = new ArrayList< T>();
if(cl instanceof Fruit)
{
//使用JDBC和SQL SELECT * FROM fruit
}
else if(cl instanceof User)
{
//使用JDBC和SQL SELECT * FROM user
}
return list;
}
}
class Fruit
{
static final Fruit myType;
static {myType = new Fruit();}
}
class User
{
static final User myType;
static {myType = new User();}
}

EDIT



我认为这个实现就像你问过



< public class Test
{
public static void main(String [] args)throws InstantiationException,IllegalAccessException
{
test dataAccess = new Test();

List< Fruit> FruitList = dataAccess.getAllRecords(Fruit.class);

列表< User> UserList = dataAccess.getAllRecords(User.class);

}
< T>列表< T> getAllRecords(Class< T> cl)throws InstantiationException,IllegalAccessException
{
T inst = cl.newInstance();
List< T> list = new ArrayList< T>();
if(inst instanceofHello)
{
//使用JDBC和SQL SELECT * FROM user
}
else if(inst instanceof User)
{
//使用JDBC和SQL SELECT * FROM fruit
}
return list;
}
}


I am designing a simple Data Access Object for my Java application. I have a few classes (records) that represents a single row in tables like User and Fruit.

I would like to have a single method for getting all records of a specific type.

For the moment I have it like this:

public List<User> getAllUsers() {
 ...
}

public List<Fruit> getAllFruits() {
 ...
}

....

But I would like to have a single polymorphic method like this (wrong):

public List<T> getAllRecords(Class<T> type) {
    if(type instanceof User) {
        // Use JDBC and SQL SELECT * FROM user
    } else if(type instanceof Fruit) {
        // Use JDBC and SQL SELECT * FROM fruit
    }
    return collection;
}

Example for uses:

List<Fruit> fruits = myDataAccessObject.getAllRecrods(Fruit.class);
List<User> users = myDataAccessObject.getAllRecords(User.class);

How can I do this in Java?

解决方案

Since you say that you don't want you data access methods in different classes(in the comment to anish's answer),I thought why not try something like this.

public class Records {

public interface RecordFetcher<T>{
    public List<T> getRecords();
}
static RecordFetcher<Fruit> Fruit=new RecordFetcher<Fruit>(){
    public List<Fruit> getRecords() {
        ...
    }
};


static RecordFetcher<User> User=new RecordFetcher<User>(){
    public List<User> getRecords() {
        ...
    }   
};

public static void main(String[] args) {
    List<Fruit> fruitRecords=Records.Fruit.getRecords();
    List<User> userRecords=Records.User.getRecords();

}
}

EDIT:

I would like to add one more of my implementation.

public class Test 
{ 
    public static void main(String[] args) 
    { 
       Test dataAccess=new Test();
       List<Fruit> FruitList=dataAccess.getAllRecords(Fruit.myType);
       List<User> UserList=dataAccess.getAllRecords(User.myType);
    } 
    <T> List<T> getAllRecords(T cl)
    {
        List<T> list=new ArrayList<T>();
        if(cl instanceof Fruit)
        {
             // Use JDBC and SQL SELECT * FROM fruit
        }
        else if(cl instanceof User)
        {
            // Use JDBC and SQL SELECT * FROM user
        }
        return list;
    }
}
class Fruit
{
    static final Fruit myType;
    static {myType=new Fruit();}
}
class User
{
    static final User myType;
    static {myType=new User();}
}

EDIT:

I think this implementation is just as you have asked

public class Test 
{ 
    public static void main(String[] args) throws InstantiationException, IllegalAccessException 
    { 
       Test dataAccess=new Test();

       List<Fruit> FruitList=dataAccess.getAllRecords(Fruit.class);

       List<User> UserList=dataAccess.getAllRecords(User.class);

    } 
    <T> List<T> getAllRecords(Class<T> cl) throws InstantiationException, IllegalAccessException
    {
        T inst=cl.newInstance();
        List<T> list=new ArrayList<T>();
        if(inst instanceof Fruit)
        {
             // Use JDBC and SQL SELECT * FROM user
        }
        else if(inst instanceof User)
        {
            // Use JDBC and SQL SELECT * FROM fruit
        }
        return list;
    }
}

这篇关于我如何传递一个类作为参数,并返回一个通用集合在Java?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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