如何使用多个构造函数删除重复的代码,同时保持可读性? [英] How can I use multiple constructors to remove duplicated code while maintaining readability?

查看:260
本文介绍了如何使用多个构造函数删除重复的代码,同时保持可读性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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
}

为了防止stuff一些作业,我尝试了像:

To prevent duplication of "stuff" and a few assignments, I tried out something like:

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
}

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

This works for what I want to do, but sometimes I need to use some lengthy code to create new objects or do some calculations:

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
}

这段代码与之前,只有正在传递的参数不是很可读。我喜欢做类似的事情:

This code is pretty much the same as before, only the parameters that are being passed aren't very readable. I would prefer doing something like:

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
}

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

Basically I'm building the parameters within the constructor body. Then I'm trying to pass those built parameters to another constructor. I think I remember being able to use the "this" and "super" keywords to call constructors while inside the body of another constructor when coding in Java. It doesn't seem possible in C#.

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

Is there a way to do this easily? Did I do something incorrectly? If this is not possible, should I just stick with the unreadable code?

我想我总是可以将重复的代码转换为完全在构造函数之外的另一个方法。

I guess I could always cut the duplicated code into another method completely outside the constructors. Then each constructor would just do its own thing and call the code shared by the other constructors.

推荐答案

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

As an alternative to calling an initialization method from all constructors (which prevents you from using readonly fields) or factory methods (which introduce additional complexity when you have derived classes), you can use a 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);
    }
}

这篇关于如何使用多个构造函数删除重复的代码,同时保持可读性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆