How can I pass a Class as parameter and return a generic collection in Java?(如何将 Class 作为参数传递并在 Java 中返回泛型集合?)
问题描述
我正在为我的 Java 应用程序设计一个简单的数据访问对象.我有一些类(记录)代表表中的单行,例如 User
和 Fruit
.
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.
暂时我是这样的:
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;
}
使用示例:
List<Fruit> fruits = myDataAccessObject.getAllRecrods(Fruit.class);
List<User> users = myDataAccessObject.getAllRecords(User.class);
如何在 Java 中做到这一点?
How can I do this in Java?
推荐答案
既然你说你不希望你在不同类中的数据访问方法(在对 anish 答案的评论中),我想为什么不尝试这样的事情.
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();
}
}
我想再添加一个我的实现.
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();}
}
编辑:
我认为这个实现就像你问的那样
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;
}
}
这篇关于如何将 Class 作为参数传递并在 Java 中返回泛型集合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将 Class 作为参数传递并在 Java 中返回泛型集合?


- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- 获取数字的最后一位 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01
- 转换 ldap 日期 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01