调用扩展类的构造函数

本文关键字:构造函数 扩展 调用 | 更新日期: 2023-09-27 17:50:08

假设我有以下类:

    class SuperModel
{
    private OracleProvider database;
    private SqlTemplates template;
    public SuperModel()
    {
        this.database = new OracleProvider();
        this.template = new SqlTemplates(database);
    }
    protected SqlTemplates getTemplate()
    {
        return this.template;
    }
}

现在我有了下面的类来扩展上面的内容:

    class Henvendelser : SuperModel
{
    public Henvendelser()
    {
    }
    public ArrayList getQueue(DateTime start, DateTime end)
    {
        String sql =
                "SELECT                                                                     " +
                "   TIDSPUNKT,                                                              " +
                "   NVL(QUEUE,' ') AS QUEUE,                                                " +
                "   NVL(SUM(ANTAL_KALD),0) AS CALLS,                                        " +
                "   NVL(SUM(ANTAL_BESVARET),0) AS ANSWERED_CALLS,                           " +
                "   NVL(SUM(BESVARET_25_SEK),0) AS ANSWERED_CALLS_25_SEC,                   " +
                "   NVL(SUM(INTERN_KALD),0) AS INTERNAL_CALLS                               " +
                "FROM                                                                       " +
                "   KS_DRIFT.PERO_NKM_KØ_OVERSIGT                                           " +
                "WHERE                                                                      " +
                "   TIDSPUNKT >= '" + start+ "' AND                                         " +
                "   TIDSPUNKT <= '" + end + "' AND                                          " +
                "   TO_CHAR(TIDSPUNKT,'DY') NOT IN ('AB')                                   " +
                "GROUP BY                                                                   " +
                "   QUEUE                                                                   " +
                "ORDER BY                                                                   " +
                "   TIDSPUNKT                                                               ";
        ArrayList result = this.getTemplate().template(sql, SqlTemplates.READ_FROM_QUERY);
        return result;
   }
}

SuperModel的构造函数被自动调用,还是我必须做一些事情来确保它被调用?

调用扩展类的构造函数

在您的特定代码中,它是隐式调用的。要显式调用它,您需要使用base关键字。

参见:使用构造函数c#

在派生类中,如果未调用基类构造函数通过使用base关键字、默认构造函数if显式地有一个是隐式的

派生类中的构造函数:

public Henvendelser()

等于

public Henvendelser() :base()

代码中的其他小观察:

  1. List<T>代替ArrayList
  2. 在查询中使用参数而不是字符串连接。参见Sql注入