C#调用扩展类的构造函数

时间:2014-03-13 13:09:47

标签: c# oop inheritance

比如说我有以下课程:

    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构造函数,还是必须执行某些操作才能确保调用它?

1 个答案:

答案 0 :(得分:5)

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

请参阅:Using Constructors C#

  

在派生类中,如果未调用基类构造函数   显式地使用base关键字,默认构造函数,if   有一个,被暗称。

所以你的构造函数在派生类中:

public Henvendelser()

等于

public Henvendelser() :base()

代码中的其他一些小观察:

  1. 使用List<T>代替ArrayList
  2. 将参数与查询一起使用而不是字符串连接。请参阅Sql Injection