我有以下类,它接受一个接口并执行一些功能:
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.");
}
}
}
传递给处理程序类的类(或接口)从上面扩展抽象类:
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;
}
}
问题是是否可以将被覆盖方法MyObjectData
的返回列表的对象类型()从类传递给处理程序类方法?getDataList()
MSSQLTaskStatement
public List<?> getDataList() throws SQLException
最好的问候, 桑德罗