【问题标题】:Classes in c# with constructor [duplicate]c#中带有构造函数的类[重复]
【发布时间】:2017-10-17 12:35:03
【问题描述】:

标题的结尾是什么意思?

MobilePhone(string phoneNumber, string name) : this(phoneNumber)
{
    this.name = name;
}

【问题讨论】:

  • 什么标题?你到底指的是哪一部分,怎么不清楚?
  • this(phoneNumber) 调用另一个只接受电话号码的构造函数。

标签: c#


【解决方案1】:

: this(phoneNumber) 调用另一个只接受电话号码(或至少一个string)的构造函数重载:

MobilePhone(string phoneNumber, string name) : this(phoneNumber)
{
    this.name = name;
}

//this one is invoked using 'this(phoneNumber)' above
MobilePhone(string phoneNumber)
{
    this.phoneNumber = name;
}

【讨论】:

    【解决方案2】:

    我猜你指的是: this(phoneNumber)

    这基本上是第二个构造函数调用。它被称为构造函数链接。

    您基本上有两个构造函数,一个调用第二个构造函数来获取其内容。

    //Constructor A gets called and calls constructor B
    MobilePhone(string number, string name) : this(number) 
    {
        this.name = name;
    }
    
    //This would be constructor B
    MobilePhone(string number)
    {
        this.number = number;
    }
    

    【讨论】:

      【解决方案3】:
      this(phoneNumber)
      

      表示'在调用以下代码之前,调用 other MobilePhone 构造函数(它采用单个 string 参数),将 phoneNumber 作为参数传递给它。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-09-22
        • 1970-01-01
        • 2015-06-17
        • 2015-12-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-22
        • 2011-03-27
        相关资源
        最近更新 更多