C#-构造函数的链调用

C#-构造函数的链调用,c#,parameters,constructor,chaining,C#,Parameters,Constructor,Chaining,我目前正在学习C#,我正在学习构造函数和构造函数的链调用,这样就不必在每个构造函数中粘贴相同的代码(变量的值相同) 我有三个构造函数,一个没有参数,一个有三个参数,一个有四个参数。我想做的是,使用默认构造函数调用具有三个参数的构造函数,传递参数(变量)的默认值,以及具有三个参数的构造函数,我想用它调用具有四个参数的构造函数。我似乎对第一个进行了排序,列出了默认值,但我正在努力如何编写包含三个参数的构造函数,然后在需要时使用四个参数调用构造函数 默认构造函数应将string类型的所有实例变量分配给

我目前正在学习C#,我正在学习构造函数和构造函数的链调用,这样就不必在每个构造函数中粘贴相同的代码(变量的值相同)

我有三个构造函数,一个没有参数,一个有三个参数,一个有四个参数。我想做的是,使用默认构造函数调用具有三个参数的构造函数,传递参数(变量)的默认值,以及具有三个参数的构造函数,我想用它调用具有四个参数的构造函数。我似乎对第一个进行了排序,列出了默认值,但我正在努力如何编写包含三个参数的构造函数,然后在需要时使用四个参数调用构造函数

默认构造函数应将string类型的所有实例变量分配给string.Empty

public Address()
{
   m_street = string.Empty;
   m_city = string.Empty;
   m_zipCode = string.Empty;
   m_strErrMessage = string.Empty;
   m_country = Countries;
}


public Address(string street, string city, string zip)
{
}

public Address(string street, string city, string zip, Countries country)
{
}
我本想做以下工作,但没有成功:-

public Address(string street, string city, string zip)
     : this street, string.Empty, city, string.Empty, zip, string.Empty
{
}
您需要():


这将调用指定了6个参数中的3个(如果有)的地址构造函数,这就是您要做的吗?

您通常应该将信息最少的构造函数链接到信息最多的构造函数上,而不是反过来。这样,每个字段可以精确地分配到一个位置:信息最多的构造函数。实际上,您已经在文章中描述了这种行为,但是您的代码做了一些完全不同的事情

您还需要为构造函数链接使用正确的语法,即:

: this(arguments)
例如:

public class Address
{
    private string m_street;
    private string m_city;
    private string m_zipCode;
    private string m_country;

    public Address() : this("", "", "")
    {
    }


    public Address(string street, string city, string zip)
        : this(street, city, zip, "")
    {
    }

    public Address(string street, string city, string zip, string country)
    {
        m_street = street;
        m_city = city;
        m_zip = zip;
        m_country = country;
    }
}

有关构造函数链接的更多信息,请参阅我不久前编写的。

您的语法很接近。试试这个

public Address(string street, string city, string zip)
      : this( street, string.Empty, city, string.Empty, zip, string.Empty )
{
}
在您的示例中,您还应该删除默认构造函数,并将变量的初始化放入链末尾的构造函数中。就是那个以你的国家为例的


希望这有帮助。

您可以使用
this
调用同一对象上的其他构造函数。一般的想法是在最复杂的构造函数中进行属性/字段的实际初始化,并逐步将更简单的构造函数链接在一起,给出默认值

假设
国家
是一个枚举,例如:

public enum Countries
{
   NotSet = 0,
   UK,
   US
}
您可以这样做(请注意,您的枚举不必具有默认状态,例如
NotSet
,但如果没有,您只需要确定如果未指定值,默认值是什么):


其想法是将实例化的逻辑留给接受最多参数的构造函数,并将其他参数用作只向该构造函数传递值的方法,这样您只需编写一次代码

试试这个:

public Address() 
  : this(String.Empty, String.Empty, String.Empty)
{
}


public Address(string street, string city, string zip) 
  : this(street, city, zip, null)
{
}

public Address(string street, string city, string zip, Countries country) 
{
    m_street = street;
    m_city = city;
    m_zipCode = zip;
    m_country = Countries;
    m_strErrMessage = string.Empty;
}

构造函数链接需要括号,如下所示:

public Address(string street, string city, string zip)
          : this (street, string.Empty, city, string.Empty, zip, string.Empty)
    {
    }

另外,您正在使用6个参数调用构造函数。

看起来您忘记了一些括号。还有几个关键字……您还需要一个类似于公共地址的构造函数(string street,string unknown1,string city,string unknown2,string zip,string unknown3)。根据您对有这些参数的构造函数的建议,它似乎不起作用:-公共地址(string street,string city,string zip):这个(street,city,zip,Nothing){}关键字'Nothing'不被接受,并引发异常。我尝试用String.Empty替换它,但也不起作用。作为第四个参数(国家)从枚举中提取字符串值,不确定这是否是问题所在。抱歉,我实际上是用Visual Basic编写代码的,因此我没有错误地编写任何东西,只需将
Nothing
替换为
null
。我已经修复了答案。但为了澄清我怀疑如果
国家
是枚举,那么
国家
类型是什么?与您的su对于有这些参数的构造函数,它似乎不起作用:-公共地址(string street,string city,string zip):这个(street,city,zip,string.Empty){}我尝试按照另一个用户的建议用'Nothing'替换'string.Empty',但这也不起作用。作为第四个参数(country)从枚举中提取字符串值,不确定这是否是问题所在,但如上所述,包含三个参数的第二个构造函数不接受“string.Empty”作为参数4的替代占位符。@Enverlap-如果
Countries
是枚举,则您只需在枚举中定义一个默认值并传递它,或者从当前枚举中选择默认值。请参阅更新的答案。
public Address() 
  : this(String.Empty, String.Empty, String.Empty)
{
}


public Address(string street, string city, string zip) 
  : this(street, city, zip, null)
{
}

public Address(string street, string city, string zip, Countries country) 
{
    m_street = street;
    m_city = city;
    m_zipCode = zip;
    m_country = Countries;
    m_strErrMessage = string.Empty;
}
public Address(string street, string city, string zip)
          : this (street, string.Empty, city, string.Empty, zip, string.Empty)
    {
    }