【问题标题】:Declaring methods with similar signatures and parameters声明具有相似签名和参数的方法
【发布时间】:2015-11-19 22:47:36
【问题描述】:

所以基本上我被要求编写代码,但正如标题所说,我在添加具有相同签名的多个字符串方法时遇到了麻烦。我必须创建一个类,其中包含客户姓名、客户代码等信息。 AB001,地址和电话号码将被存储。所以换句话说,我必须使用字符串。我的问题是:是否有另一种编码方式或解决问题的方法?

这就是代码的样子:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace myapp2
{
    internal class Customer
    {
        private string name;
        private string address;
        private string customerCode;
        private string phoneNumber;

        public Customer(string nameofacc)
        {
            name = nameofacc;
        }

        public string getName()
        {
            return name;
        }

        public void setName(string nameofacc)
        {
            name = nameofacc;
        }

        public Customer(string code)
        {
            customerCode = code;
        }

        public string getCustomerCode()
        {
            return customerCode;
        }

        public void setCustomerCode(string code)
        {
            customerCode = code;
        }
        public Customer(string customeraddress)
        {
            address = customeraddress;
        }

        public string getAddress()
        {
            return address;
        }

        public void setAddress(string customeraddress)
        {
            address = customeraddress;
        }
        public Customer(string number)
        {
            phoneNumber = number;
        }

        public string getPhoneNumber()
        {
            return phoneNumber;
        }

        public void setPhoneNumber(string number)
        {
            phoneNumber = number;
        }
    }
}

这些是我得到的错误:

错误1:类型'myapp2.Customer'已经定义了一个名为'Customer'的成员,具有相同的参数类型

Error2:类型“myapp2.Customer”已经定义了一个名为 具有相同参数类型的“客户”

Error3:类型“myapp2.Customer”已经定义了一个名为 具有相同参数类型的“客户”

我对使用 C# 不是很有经验,所以如果有人可以帮忙,请帮忙!

【问题讨论】:

  • 错误1:类型'myapp2.Customer'已经定义了一个名为'Customer'的成员,具有相同的参数类型错误2:类型'myapp2.Customer'已经定义了一个名为'Customer'的成员,具有相同的参数类型错误3:类型'myapp2.Customer'已经定义了一个名为'Customer'的成员具有相同的参数类型这些是我得到的错误..

标签: c# methods overloading


【解决方案1】:

使用properties:

属性是提供灵活机制来读取、写入或计算私有字段值的成员。

private string address;

public string Address
{
     get { return address; }
     set { address = value; }
}

Auto-Implemented Properties:

public string Address { get; set; }

【讨论】:

    【解决方案2】:

    您可能应该使用 1 个构造函数并手动使用默认值在构造函数中传递的参数名称:

    public Customer(string nameofacc = null, string customerAddress = null, string number = null, string code = null)
            {
                name = nameofacc;
                address = customerAddress;
                customerCode = code;
                phoneNumber = number;
            }
    

    你可以在代码中使用它,像这样在任何参数组合中:

    var customer = new Customer(number: "12345");
    var anotherCustomer = new Customer(code: "123", customerAddress:"Stub Address");
    

    对于您的 get/set 方法,请考虑使用属性而不是方法对:https://msdn.microsoft.com/en-us/library/x9fsa0sw.aspx

    在这种情况下,您将能够像这样创建类:

    var customer = new Customer {
          NameOfAcc = "John Doe",
          Phone = "123 456 7890"
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-22
      • 2012-12-28
      • 1970-01-01
      • 1970-01-01
      • 2016-07-08
      相关资源
      最近更新 更多