Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过构造函数重载指定属性_C#_Oop_Constructor - Fatal编程技术网

C# 通过构造函数重载指定属性

C# 通过构造函数重载指定属性,c#,oop,constructor,C#,Oop,Constructor,在下面的类中,我想为构造函数1指定color为color.White;当通过构造函数2调用时,应该为其分配参数值。但在这样做时,它首先调用构造函数1,构造函数1依次将color指定为color.White,然后指定所需的值 当涉及到许多构造函数并包含对象时,问题就变得合理了 有没有办法处理这些不必要的步骤?我想我错过了一些基本的东西 public class Image { Texture2D texture; Rectangle frame; Rectangle off

在下面的类中,我想为构造函数1指定
color
color.White
;当通过构造函数2调用时,应该为其分配参数值。但在这样做时,它首先调用构造函数1,构造函数1依次将
color
指定为
color.White
,然后指定所需的值

当涉及到许多构造函数并包含对象时,问题就变得合理了

有没有办法处理这些不必要的步骤?我想我错过了一些基本的东西

public class Image
{
    Texture2D texture;
    Rectangle frame;
    Rectangle offsetBound;
    Color color;
    // Constructor 1
    public Image(Texture2D texture, Rectangle frame, Rectangle offsetBound)
    {
        this.texture = texture;
        this.frame = frame;
        this.offsetBound = offsetBound;
        this.color = Color.White;  // This is irrelevant
    }
    // Constructor 2
    public Image(Texture2D texture, Rectangle frame, Rectangle offsetBound, Color color)
        : this(texture, frame, offsetBound)
    {
        this.color = color;
    }
}

您可以重新安排如下内容:

// Constructor 1
public Image(Texture2D texture, Rectangle frame, Rectangle offsetBound)
    : this(texture, frame, offsetBound, Color.White)
{ }

// Constructor 2
public Image(Texture2D texture, Rectangle frame, Rectangle offsetBound, Color color)        
{
    this.texture = texture;
    this.frame = frame;
    this.offsetBound = offsetBound;
    this.color = color;
}

您可以重新安排如下内容:

// Constructor 1
public Image(Texture2D texture, Rectangle frame, Rectangle offsetBound)
    : this(texture, frame, offsetBound, Color.White)
{ }

// Constructor 2
public Image(Texture2D texture, Rectangle frame, Rectangle offsetBound, Color color)        
{
    this.texture = texture;
    this.frame = frame;
    this.offsetBound = offsetBound;
    this.color = color;
}

您还可以执行下一步,删除第一个构造函数,只留下一个这样的构造函数,将提供相同的结果:

public Image(Texture2D texture, Rectangle frame, Rectangle offsetBound, Color? col = null)
{
    this.color = col ?? Color.White;
    this.texture = texture;
    this.frame = frame;
    this.offsetBound = offsetBound;
}

通过使用可选参数,您可以获得与2个构造函数相同的结果,如果用户不想提供颜色,请不要,并将其设置为默认的color.White。

您也可以执行下一步操作,删除第一个构造函数,只留下一个构造函数,这样也可以提供相同的结果:

public Image(Texture2D texture, Rectangle frame, Rectangle offsetBound, Color? col = null)
{
    this.color = col ?? Color.White;
    this.texture = texture;
    this.frame = frame;
    this.offsetBound = offsetBound;
}

通过使用可选参数,您可以获得与2个ctor相同的结果,如果用户不想提供颜色,请不要提供,并将其设置为默认的color.White。

为什么不让ctor 1调用ctor 2并传递白色?听起来不错!我没想到。非常感谢。为什么不让ctor 1呼叫ctor 2并传递白色?听起来不错!我没想到。非常感谢。