【问题标题】:C# - Chain Calling of ConstructorC# - 构造函数的链式调用
【发布时间】:2011-08-04 16:03:38
【问题描述】:

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

【问题讨论】:

  • 看起来你忘记了一些括号。还有几个关键词……

标签: c# parameters constructor chaining


【解决方案1】:

您通常应该将具有最少信息的构造函数链接到具有最多信息的构造函数上,而不是相反。这样,每个字段都可以精确地分配在一个地方:具有最多信息的构造函数。您实际上在帖子中描述了这种行为 - 但随后您的代码做了一些完全不同的事情。

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

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

有关构造函数链接的更多信息,请参阅我前一阵子写的this article

【讨论】:

    【解决方案2】:

    你需要 ():

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

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

    【讨论】:

    • 你还需要一个类似public Address(string street, string unknown1, string city, string unknown2, string zip, string unknown3)的构造函数。
    【解决方案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 替换它,但这也不起作用。由于第四个参数(国家/地区)从 Enum 中提取字符串值,因此不确定这是否是问题所在。
    • 抱歉,我实际上是用 Visual Basic 编写代码,所以我写错了,只需将 Nothing 替换为 null。我已经确定了答案。
    • 但澄清一下,如果Countries 是枚举,我怀疑它是否有效,Countries 类型是什么?
    【解决方案4】:

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

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

    在您的示例中,您还应该删除默认构造函数并将变量的初始化放入链末尾的构造函数中。在你的例子中,那个国家就是那个国家。

    希望这会有所帮助。

    【讨论】:

      【解决方案5】:

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

      假设Countries是一个Enum,如:

      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 - 如果Countries 是一个枚举,那么你只需要在你的枚举中定义一个默认值并传递它,或者从你当前的枚举中选择一个默认值。查看更新的答案。
      【解决方案6】:

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-23
        • 2016-06-07
        • 2013-11-02
        相关资源
        最近更新 更多