C# - 构造函数的链式调用

4

我目前正在学习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
{
}

看起来你忘记了一些括号和几个关键字... - Igby Largeman
6个回答

11

通常情况下,你应该先调用传入参数最少的构造函数,再调用传入参数最多的构造函数,这样每个字段就只会被赋值一次:在传入参数最多的构造函数中。你实际上在帖子中描述了这种行为,但是你的代码却完全不同。

你还需要使用正确的构造函数链接语法,如下所示:

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

关于构造函数链的更多信息,请参阅我一段时间前写的这篇文章


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

你是想用指定的3个参数调用Address构造函数吗?(如果有的话)


你还需要一个构造函数,它看起来像 public Address(string street, string unknown1, string city, string unknown2, string zip, string unknown3) - Chris Shouts

3

这个想法是将实例化的逻辑留给参数最多的构造函数,并使用其他构造函数作为仅传递值到该构造函数的方法,这样你只需编写一次代码。

尝试这样做:

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, city, zip, Nothing) { }关键字“Nothing”不被接受并引发异常。我尝试用String.Empty替换它,但也不起作用。由于第四个参数(国家/地区)从枚举中提取字符串值,所以不确定是否是问题所在。 - Enverlap
抱歉,我实际上是用 Visual Basic 编写的,所以我错误地写了 Nothing,请将其替换为 null。我已经修正了答案。 - PedroC88
但是为了澄清,如果Countries是一个枚举类型,我怀疑那样做行不通,Countries的类型是什么? - PedroC88

1

你的语法接近正确。试试这个

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

在你的例子中,你还应该删除默认构造函数,并将变量的初始化放入链末端的构造函数中。在你的例子中,那个构造函数需要传入一个国家。
希望这可以帮到你。

1

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

假设Countries是一个枚举类型,例如:

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,Countries.NotSet)
{
}

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

根据你建议的有三个参数的构造函数,它似乎无法工作:-public Address(string street, string city, string zip) : this(street, city, zip, String.Empty) { }我尝试将“String.Empty”替换为另一个用户建议的“Nothing”,但也不起作用。由于第四个参数(国家)从枚举中提取字符串值,不确定是否存在问题,但正如所说,包含三个参数的第二个构造函数不接受“String.Empty”作为参数4的替代占位符。 - Enverlap
@Enverlap - 如果Countries是一个枚举类型,那么你只需要在你的枚举中定义一个默认值并传递它,或者从你当前的枚举中选择一个默认值。请参见更新后的答案。 - Jamiec

0

你需要使用括号进行构造函数链式调用,像这样:

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

另外,你正在调用一个有6个参数的构造函数。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接