根据变量多态分配一个类

时间:2019-10-30 18:27:30

标签: java

我有一个抽象类Foo,其中一个BarsBarz以及其他一些持有一些数据的类。

我将数据存储到SQLite中的几个不同的表中。我还写了一个小类,基本上包含它“创建”的java类,sqlite主数据库中表的名称以及每列名称和列数据类型的一对,以帮助在应用程序运行时快速将记录变成对象。运行。

我想使事物尽可能抽象,以实现可重用性。如何仅通过从上方获取“该表”中的一条记录的table信息来创建具有正确数据类型的对象的正确子类,而无需在每个类上进行O(n)循环(这很快,但是每个类的代码也很难编码,而且打字很多。)

SQLiteTable类:

class SQLiteTable<T>
{
    ...
    SQLiteTable(String name, SQLiteColumn...columns)
    ...
}

JDBC SQLite抽象请求帮助:

...
public ArrayList<Foo> query(SQLiteTable table, String statement)
{
    safeString = purgeOfFilt(statement);
    statement = connection.prepareStatement(safeString);
    results = statement.executeQuery();
    ArrayList<Foo> records = new ArrayList<>();
    while(results.next())
    {
        ... // for loop to extract field information about the object I'd like to create
        records.add(/*I'm not sure how to create the correct instance of the class here 
                     (with the extracted fields--some of which are final at creation)*/));
    }
}
...

0 个答案:

没有答案