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

标签 c# parameters constructor this code-duplication

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
}

为了防止重复“东西”和一些作业,我尝试了类似的方法:

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
}

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

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
}

此代码与之前的代码几乎相同,只是传递的参数不太可读。我宁愿做这样的事情:

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
}

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

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

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

最佳答案

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

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);
    }
}

关于c# - 如何在保持可读性的同时使用多个构造函数来删除重复代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10377888/

相关文章:

c# - 从 Validate 函数返回额外信息的最佳实践

c# - WCF 数据服务 : How to query ALL entities when Paging is enabled?

传递字符串时,Java 构造函数可变参数冲突

c# - 如何在Xamarin Forms中的Page.Toolbaritems之间添加行分隔符

c# - byte[] 到 MVC 3 中的文件类型

javascript - 使用js ajax的参数调用c#方法

ruby - 我可以告诉 Ruby 方法期望特定的参数类型吗?

参数在双括号中的 Ruby 方法

java - 在 Java 的构造函数中尝试和捕获 block 是一种好习惯吗

c++ - 派生类类型与基类的兼容性会导致内存泄漏吗?