如何在保持可读性的同时使用多个构造函数来删除重复的代码?

Che*_*ese 40 c# parameters constructor code-duplication this

int a, b, c;

Constructor()
{
    a = 5;
    b = 10;
    c = 15;
    //do stuff
}
Constructor(int x, int y)
{
    a = x;
    b = y;
    c = 15;
    //do stuff
}
Constructor(int x, int y, int z)
{
    a = x;
    b = y;
    c = z;
    //do stuff
}
Run Code Online (Sandbox Code Playgroud)

为了防止重复"东西"和一些任务,我尝试了类似的东西:

int a, b, c;

Constructor(): this(5, 10, 15)
{
}
Constructor(int x, int y): this(x, y, 15)
{
}
Constructor(int x, int y, int z)
{
    a = x;
    b = y;
    c = z;
    //do stuff
}
Run Code Online (Sandbox Code Playgroud)

这适用于我想要做的事情,但有时我需要使用一些冗长的代码来创建新对象或进行一些计算:

int a, b, c;

Constructor(): this(new Something(new AnotherThing(param1, param2, param3),
    10, 15).CollectionOfStuff.Count, new SomethingElse("some string", "another
    string").GetValue(), (int)Math.Floor(533 / 39.384))
{
}
Constructor(int x, int y): this(x, y, (int)Math.Floor(533 / 39.384))
{
}
Constructor(int x, int y, int z)
{
    a = x;
    b = y;
    c = z;
    //do stuff
}
Run Code Online (Sandbox Code Playgroud)

此代码与以前几乎相同,只是传递的参数不是很易读.我宁愿做类似的事情:

int a, b, c;

Constructor(): this(x, y, z) //compile error, variables do not exist in context
{
    AnotherThing at = new AnotherThing(param1, param2, param3);
    Something st = new Something(aThing, 10, 15)
    SomethingElse ste = new SomethingElse("some string", "another string");

    int x = thing.CollectionOfStuff.Count;
    int y = ste.GetValue();
    int z = (int)Math.Floor(533 / 39.384);

    //In Java, I think you can call this(x, y, z) at this point.
    this(x, y, z); //compile error, method name expected
}
Constructor(int x, int y): this(x, y, z) //compile error
{
    int z = (int)Math.Floor(533 / 39.384);
}
Constructor(int x, int y, int z)
{
    a = x;
    b = y;
    c = z;
    //do stuff
}
Run Code Online (Sandbox Code Playgroud)

基本上我正在构造函数体中构建参数.然后我试图将这些构建的参数传递给另一个构造函数.我想我记得当用Java编码时,能够使用"this"和"super"关键字在另一个构造函数体内调用构造函数.在C#中似乎不可能.

有办法轻松做到这一点吗?我做错了什么吗?如果这是不可能的,我应该坚持使用不可读的代码吗?

我想我总是可以将重复的代码完全切换到构造函数之外的另一种方法.然后每个构造函数只做自己的事情并调用其他构造函数共享的代码.

Mic*_*Liu 39

作为从所有构造函数调用初始化方法(阻止您使用readonly字段)或工厂方法(在派生类时引入额外复杂性)的替代方法,您可以使用参数对象:

int a, b, c;

public Constructor()
    : this(new ConstructorParameters())
{
}

public Constructor(int x, int y)
    : this(new ConstructorParameters(x, y))
{
}

public Constructor(int x, int y, int z)
{
    a = x;
    b = y;
    c = z;
    //do stuff 
} 

private Constructor(ConstructorParameters parameters)
    : this(parameters.X, parameters.Y, parameters.Z)
{
}

private class ConstructorParameters
{
    public int X;
    public int Y;
    public int Z;

    public ConstructorParameters()
    {
        AnotherThing at = new AnotherThing(param1, param2, param3); 
        Something st = new Something(at, 10, 15) 
        SomethingElse ste = new SomethingElse("some string", "another string"); 

        X = st.CollectionOfStuff.Count; 
        Y = ste.GetValue(); 
        Z = (int)Math.Floor(533 / 39.384); 
    }

    public ConstructorParameters(int x, int y)
    {
        X = x;
        Y = y;
        Z = (int)Math.Floor(533 / 39.384);
    }
}
Run Code Online (Sandbox Code Playgroud)


Eri*_*ips 20

你可以做

Constructor() : this(5, 10, 15)
{
}
Constructor(int x, int y) : this(x, y, 15)
{
}
Constructor(int x, int y, int z)
{
  int a = x;
  int b = y;
  int c = z;
  //do stuff
}
Run Code Online (Sandbox Code Playgroud)

但是,如果您需要根据参数做一些奇特的逻辑,我会使用工厂模式:

public class myclass
{
  private myclass(int x, int y, int z)
  {
    int a = x;
    int b = y;
    int c = z;
    //do stuff
  }
  public static myclass Create()
  {
    AnotherThing at = new AnotherThing(param1, param2, param3);
    Something st = new Something(aThing, 10, 15)
    SomethingElse ste = new SomethingElse("some string", "another string");

    int x = thing.CollectionOfStuff.Count;
    int y = ste.GetValue();
    int z = (int)Math.Floor(533 / 39.384);

    return new myclass(x, y ,z);
  }
  public static myclass Create(int x, int y)
  {
    if (x = 1)
      return new myclass(x, y, 2)
    else 
      return new myclass(x, y, 15);
  }
  public static myclass Create(int x, int y, int z)
  {
    //so on and so forth
    return new myclass(x, y, z);
  }
}
Run Code Online (Sandbox Code Playgroud)

虽然您不需要工厂模式,但它肯定会使您的构造函数逻辑可读.