C#中的构造函数链接

时间:2016-11-08 15:52:20

标签: c# c#-4.0

我正在尝试链接构​​造函数。

public class Method1
{
    public Method1(string a, string b) : this(a, b, "", "")
    {

    }

    public Method1(string c, string d) : this("", "", c, d)
    {

    }

    public Method1(string a, string b, string c, string d)
    {

    }
} 

有什么方法可以实现这个目标吗?目前它显示编译时错误。

5 个答案:

答案 0 :(得分:4)

这种事情的常见方法是拥有一个私有构造函数,它具有您需要的所有参数,然后编写一组命名良好的公共静态方法,使用适当的参数调用私有构造函数。

这是一个很好的方法,因为你可以为静态创建者方法提供合理的名称,而使用构造函数则不能(当然)。

例如:

public class Method1
{
    public static Method1 Method1FromAB(string a, string b)
    {
        return new Method1(a, b, "", "");
    }

    public static Method1 Method1FromCD(string c, string d)
    {
        return new Method1("", "", c, d);
    }

    private Method1(string a, string b, string c, string d)
    {

    }
}

答案 1 :(得分:2)

您使用的是C#4,为什么不使用可选参数?

 public Method1(string a = "", string b = "", string c = "", string d = "")

您可以将您想要的行为称为:

Method1(c: "some value", d: "some other value");

Method1(a: "some other other value", b:"another other value");

......或参数的任意组合。

答案 2 :(得分:1)

public Method1(string a, string b) : this(a, b, "", "")

public Method1(string c, string d) : this("", "", c, d)

具有相同的方法签名

您可以执行此操作,但不能使用与构造函数相同的方法签名

答案 3 :(得分:1)

public Method1(string a, string b) : this(a, b, "", "")
{

}

public static Method1 CreateMethodWithEndParams(string c, string d)
{
    Method1 method = new Method1("", "", c, d);

    return method;
}

public Method1(string a, string b, string c, string d)
{

}

你可以拥有这样的东西。当您想用c,d

创建对象时
 Method1 method = Method1.CreateMethodWithEndParams("c", "d");

答案 4 :(得分:-1)

根据变量 a、b、c 和 d 代表的类型以及您如何使用它们,您可以为它们制作结构体。 然后你可以为构造函数创建重载,这样会更容易理解。

using System;

namespace ConstructorChaining
{
    class Program
    {
        static void Main(string[] args)
        {
            var chainerAB = new ConstructorChainer(new A("aa"), new B("bb"));
            Console.WriteLine($"chainerAB - A:{chainerAB.A.Value}, B:{chainerAB.B.Value}, C:{chainerAB.C.Value}, D:{chainerAB.D.Value}");
    
            var chainerCD = new ConstructorChainer(new C("cc"), new D("dd"));
            Console.WriteLine($"chainerCD - A:{chainerCD.A.Value}, B:{chainerCD.B.Value}, C:{chainerCD.C.Value}, D:{chainerCD.D.Value}");
        }
    }


    public class ConstructorChainer
    {
        public A A { get; set; }
        public B B { get; set; }
        public C C { get; set; }
        public D D { get; set; }


        public ConstructorChainer(A a, B b) : this(a, b, new C(""), new D("")){}
    
        public ConstructorChainer(C c, D d) : this(new A(""),  new B(""), c, d){}

        private ConstructorChainer(A a, B b, C c, D d)
        {
            A = a;
            B = b;
            C = c;
            D = d;
        }
    }

    public struct A
    {
        public A(string value)
        {
            Value = value;
        }

        public string Value{ get; set;}
    }

    public struct B
    {
        public B(string value)
        {
            Value = value;
        }

        public string Value{ get; set;}
    }

    public struct C
    {
        public C(string value)
        {
            Value = value;
        }

        public string Value{ get; set;}
    }

    public struct D
    {
        public D(string value)
        {
            Value = value;
        }

        public string Value{ get; set;}
    }
}
相关问题