声明具有相似签名和参数的方法

Declaring methods with similar signatures and parameters

所以基本上我被要求编写代码,但正如标题所说,我在添加多个具有相同签名的字符串方法时遇到了问题。我必须创建一个 class,其中包含客户姓名、客户代码等信息,例如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;
        }
    }
}

这些是我遇到的错误:

Error1: Type 'myapp2.Customer' already defines a member called 'Customer' with the same parameter types

Error2: Type 'myapp2.Customer' already defines a member called 'Customer' with the same parameter types

Error3: Type 'myapp2.Customer' already defines a member called 'Customer' with the same parameter types

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

使用properties:

A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field.

private string address;

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

Auto-Implemented Properties:

public string Address { get; set; }

您可能应该使用 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

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

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