如何正确创建子类的新实例

时间:2018-12-08 20:33:02

标签: java

我有几个对象是特定类(ValidateVO)的子类。所有这些都以验证表上的类型代码存储在我们的数据库中。我想创建一个方法,可以为获取相关信息的每个此类调用。我不确定要创建实例化的类的类型,或者我不知道如何为此正确声明泛型

private List<? extends ValidateVO> getValidateSubclassByType(Class<? extends ValidateVO> clazz, String type) {
    List<? extends ValidateVO> validates = new ArrayList<>();

    StringBuilder bldValidates = new StringBuilder()
    .append("SELECT VALIDATE_ID, VALIDATE_DESCRIPTION, VALIDATE_ACTIVE ")
    .append("FROM tblVALIDATE WHERE VALIDATE_TYPE = ?");

    try (
            Connection con = dataSource.getConnection();
            PreparedStatement pstmtValidates = con.prepareStatement(bldValidates.toString());
            ) {
        pstmtValidates.setString(1, type);
        ResultSet rsWholesalerGroups = pstmtValidates.executeQuery();
        if (rsWholesalerGroups.isBeforeFirst()) {
            while (rsWholesalerGroups.next()) {
                // if I use ValidateVO here, I have a problem when I add it to the list
                ValidateVO validate = clazz.newInstance();
                validate.setId(rsWholesalerGroups.getInt("VALIDATE_ID"));
                validate.setName(rsWholesalerGroups.getString("VALIDATE_DESCRIPTION"));
                validate.setActive(rsWholesalerGroups.getString("VALIDATE_ACTIVE").equals("Y"));
                // The following is the line with the error
                // The method add(capture#2-of ? extends ValidateVO) in the type List<capture#2-of ? extends ValidateVO> is not applicable for the arguments (ValidateVO)
                validates.add(validate);
            }
        } else {
            System.out.println("No WHOLESALERS");
        }
    } catch (SQLException ex) {
        ex.printStackTrace();
    }

    return validates;
}

1 个答案:

答案 0 :(得分:1)

您不需要通配符类型。该方法可以定义通用参数,并将其用于参数和返回类型。在newInstance上调用的Class<T>返回T

private <T extends ValidateVO> List<T> getValidateSubclassByType(Class<T> clazz, String type) {
    List<T> validates = new ArrayList<>();

    StringBuilder bldValidates = new StringBuilder()
            .append("SELECT VALIDATE_ID, VALIDATE_DESCRIPTION, VALIDATE_ACTIVE ")
            .append("FROM tblVALIDATE WHERE VALIDATE_TYPE = ?");

    try (Connection con = dataSource.getConnection();
            PreparedStatement pstmtValidates = con.prepareStatement(bldValidates.toString());) {
        pstmtValidates.setString(1, type);
        ResultSet rsWholesalerGroups = pstmtValidates.executeQuery();
        if (rsWholesalerGroups.isBeforeFirst()) {
            while (rsWholesalerGroups.next()) {
                T validate = clazz.newInstance();
                validate.setId(rsWholesalerGroups.getInt("VALIDATE_ID"));
                validate.setName(rsWholesalerGroups.getString("VALIDATE_DESCRIPTION"));
                validate.setActive(rsWholesalerGroups.getString("VALIDATE_ACTIVE").equals("Y"));
                validates.add(validate);
            }
        } else {
            System.out.println("No WHOLESALERS");
        }
    } catch (SQLException ex) {
        ex.printStackTrace();
    }

    return validates;
}