动态指定返回数组列表的对象类型 [英] Specify object type of a returned array list dynamically

查看:65
本文介绍了动态指定返回数组列表的对象类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下类,它接受一个接口并执行一些功能:

I have following class which takes an interface and execute some functions:

public class MSSQLHandler {

    IMSSQLStatement statement;

    public MSSQLHandler(IMSSQLStatement statement) {
        this.statement = statement;
    }

    public void invoke() throws SQLException {
        statement.executeStatement();
    }

    public List<?> getDataList() throws SQLException {
        return statement.getDataList();
    }
}

接口由抽象类实现:

public abstract class MSSQLStatement implements IMSSQLStatement {

    protected Connection conn = null;
    protected ResultSet rs = null;

    protected abstract String createStatement() throws SQLSyntaxErrorException;

    public MSSQLStatement(Connection conn) {    
       this.conn = conn;
    }

    public void executeStatement() throws SQLException {    
       Statement st = conn.createStatement();
       String sql = createStatement();
       if(sql != null) {
          rs = st.executeQuery(createStatement());
       } else {
          throw new SQLException("Method 'createStatement()' has to be implemented.");
       }
    }    
}

传递给处理程序类的类(或接口)从上面扩展抽象类:

The class (or the interface) which is passed to the handler class extend the abstract class from above:

public class MSSQLTaskStatement extends MSSQLStatement {

    public MSSQLTaskStatement(Connection conn) {
    super(conn);
    }

    private String projectName = null;

    public void setProjectName(String projectName) {
    this.projectName = projectName;
    }

    protected String createStatement() throws SQLSyntaxErrorException {
      // Create SQL query
    }

    @Override
    public List<MyObjectData> getDataList() throws SQLException {
      // Wrap results into a data object and save it to an array list
      List<MyObjectData> l = new ArrayList<MyObjectData>()
      while(rs.next()) {
         MyObjectData o = new MyObjectData();
         o.setColumn1(rs.getString(1))
         l.add(o);
      }
      return l;
    }
}

问题是是否可以从类 MSSQLTaskStatement 传递重写的 getDataList() 方法的返回列表的对象类型(MyObjectData) 到处理程序类 public ListgetDataList() 抛出 SQLException 方法 ?

The question is whether it is possible to pass the object type (MyObjectData) of the returned list of the overridden getDataList() method from the class MSSQLTaskStatement to the handler class public List<?> getDataList() throws SQLException method ?

最好的问候,桑德罗

推荐答案

添加类型参数 IMSSQLStatementMSSQLStatement,将IMSSQLStatement中的getDataList方法改为List;getDataList() 并使用 public class MSSQLTaskStatement extends MSSQLStatement.

Add a type parameter <T> or <T extends ObjectDataBaseClass> to IMSSQLStatement and MSSQLStatement, change the method getDataList in IMSSQLStatement to List<T> getDataList() and use public class MSSQLTaskStatement extends MSSQLStatement<MyObjectData>.

然后,如果您的 MSSQLHandler 有一个字段 IMSSQLStatement;语句,它自己的getDataList() 可以类型安全地返回一个List(或者你也可以使MSSQLHandler 泛型化),如果您想将它与不基于 MyObjectData 的语句一起使用).

Then, if your MSSQLHandler has a field IMSSQLStatement<MyObjectData> statement, its own getDataList() can type-safely return a List<MyObjectData> (or you can make MSSQLHandler generic too, if you want to use it with statements that do not build on MyObjectData).

这篇关于动态指定返回数组列表的对象类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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