在C#中更改构造函数链接的顺序

时间:2013-12-19 20:45:34

标签: c# constructor chaining

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

namespace ContructorChaining
{
    class AdditionOdMultipleNum
    {
        public AdditionOdMultipleNum(int a)
        {
            Console.WriteLine("sum of {0},1 is : {1}", a, (a + 1));

        }
        public AdditionOdMultipleNum(int a,int b)
            :this(a)
        {
            Console.WriteLine("sum of {0},{1} is : {2}", a, b, (a + b));
        }
        public AdditionOdMultipleNum(int a,int b,int c)
            :this(a ,b)
        {
            Console.WriteLine("sum of {0},{1},{2} is : {3}", a, b, c, (a + b + c));
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            AdditionOdMultipleNum addnum = new AdditionOdMultipleNum(2, 2, 6);

            Console.ReadKey(true);
        }
    }
}

在上面的例子中,程序显示的输出是3,4,10 那么,是否有可能看到时尚10,4,3

的输出

换句话说,我可以改变构造函数的执行顺序,即带有3个参数的构造函数(带输出),然后调用带有2个参数的构造函数(带输出),最后调用带有1个参数的构造函数(带输出)

3 个答案:

答案 0 :(得分:1)

将所有构造函数卸载到方法中,从而允许您通过让方法在方法体中随意调用而提供所需的确切语义:

class AdditionOdMultipleNum
{
    public AdditionOdMultipleNum(int a)
    {
        Init(a);
    }
    public AdditionOdMultipleNum(int a, int b)
    {
        Init(a, b);
    }
    public AdditionOdMultipleNum(int a, int b, int c)
    {
        Init(a, b, c);
    }

    private void Init(int a)
    {
        Console.WriteLine("sum of {0},1 is : {1}", a, (a + 1));
    }
    private void Init(int a, int b)
    {
        Console.WriteLine("sum of {0},{1} is : {2}", a, b, (a + b));
        Init(a);
    }
    private void Init(int a, int b, int c)
    {
        Console.WriteLine("sum of {0},{1},{2} is : {3}", a, b, c, (a + b + c));
        Init(a, b);
    }
}

答案 1 :(得分:0)

不可能。当构造函数链接到另一个构造函数时,该另一个构造函数的主体在第一个构造函数的主体之前运行。这是无法改变的。

这看起来像是一个简化/人为的例子(你可能已经将它简化为发布所以它很容易理解,这是一件好事,谢谢你);也许如果你可以描述你的真实场景,有人可能会建议进行调整或设计更改,使其表现得如你所愿?

答案 2 :(得分:0)

这种链接不是直接可能的;但是你可以用工厂方法完成类似的事情:

class AdditionOdMultipleNum
{
    private AdditionOdMultipleNum() {}
    // A private constructor means that only methods 
    // within this class can construct new object directly
    // so let's make some factory methods:

    public static AdditionOdMultipleNum Create(int a)
    {
        Console.WriteLine("sum of {0},1 is : {1}", a, (a + 1));
        AdditionOdMultipleNum instance = new AdditionOdMultipleNum();
        // you can modify the instance here
        return instance;

    }
    public static AdditionOdMultipleNum Create(int a,int b)
    {
        Console.WriteLine("sum of {0},{1} is : {2}", a, b, (a + b));
        AdditionOdMultipleNum instance = Create(a);
        // you can modify the instance here
        return instance;

    }
    public static AdditionOdMultipleNum Create(int a,int b,int c)
    {
        Console.WriteLine("sum of {0},{1},{2} is : {3}", a, b, c, (a + b + c));
        AdditionOdMultipleNum instance = Create(a, b);
        // you can modify the instance here
        return instance;
    }
}

现在你可以打电话了

 AdditionOdMultipleNum addnum = AdditionOdMultipleNum.Create(2, 2, 6);

这将导致输出按所需顺序10, 4, 3