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 中返回泛型集合?


基础教程推荐
- 多个组件的复杂布局 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- Java Swing计时器未清除 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 大摇大摆的枚举 2022-01-01
- 从 python 访问 JVM 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01