How should I chain the constructors in a class hierarchy?

拈花ヽ惹草 提交于 2020-01-23 05:41:49

问题


We have the following class hierarchy:

public class Base
{
    public Base()
    {
        // do generic initialization 
    }

    public Base(SomeClass param1) : this()
    {
        // init properties that require param1
    }

    public Base(SomeClass param1, OtherClass param2) : this(param1)
    {
        // init properties that require param2
    }

    // ...
}

public class Derived : Base
{
    public Derived()
    {
        // do custom initialization 
    }

    public Derived(SomeClass param1) : this() // ???
    {
        // do custom initialization using param1
    }

    public Derived(SomeClass param1, OtherClass param2) : this(param1) // ???
    {
        // do custom initialization using param2
    }

    // ...
}

We would require Derived to run both its own initialization routines, up the chain, and the corresponding ones from the base class. How do we chain the constructors without duplicating code/running some of the constructors twice?


回答1:


In the derived class chain the constructors with the least parameters to the constructor with the most parameters, and then the derived constructor with the most parameters is chained to base. Something like this:

public class Base 
{
  public Base() : this(null, null)
  {
  }
  public Base(SomeClass param1) : this(param1, null)
  {
  }
  public Base(SomeClass param1, OtherClass param2)
  {
    if (param1 != null)
    {
      // initialise param1
    }
    if (param2 != null)
    {
      // initialise param2
    }
  }
}

public class Derived : Base
{
  public Derived() : this(null, null)
  {
  }
  public Derived(SomeClass param1) : this(param1, null)
  {
  }
  public Derived(SomeClass param1, OtherClass param2) : base(param1, param2)
  {
  }
} 

Depending on the context, it may be better to use default(T) instead of null to indicate a missing/default value.




回答2:


You generally chain the constructor with least, to the one with most, like this:

public Derived(SomeClass param1) : this(param1, param2)
{}

See this article on Constructors in C# for more information.

Edit:

As per @Scott below:

The one with the most parameters would then would be public Derived(SomeClass param1, OtherClass param2) : base(param1, param2) and you put your initialization code in the 2 parameter constructor in the derived and the base

To demonstrate that all constructors are called, I drafted a program:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    public class Base
    {
        public Base()
            : this(null, null)
        {
        }
        public Base(string param1)
            : this(param1, null)
        {
        }
        public Base(string param1, string param2)
        {
            Console.WriteLine("Base Class: " + param1 + "+" + param2);

            if (param1 != null)
            {
                // initialise param1
            }
            if (param2 != null)
            {
                // initialise param2
            }
        }
    }

    public class Derived : Base
    {
        public Derived()
            : this("", "")
        {
        }
        public Derived(string param1)
            : this(param1, "")
        {
        }
        public Derived(string param1, string param2)
            : base(param1, param2)
        {
            Console.WriteLine("Derived Class: " + param1 + "+" + param2);
        }
    } 
    class Program
    {
        static void Main(string[] args)
        {
            Derived d = new Derived("test1", "test2");
            Console.ReadLine();
        }
    }
}

Output:

Base class: test1+test2
Derived class: test1+test2


来源:https://stackoverflow.com/questions/7028572/how-should-i-chain-the-constructors-in-a-class-hierarchy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!