C# 声明具有相似签名和参数的方法
因此,基本上我被要求编写代码,但正如标题所说,我在添加具有相同签名的多个字符串方法时遇到了困难。我必须创建一个类,在其中存储客户名称、客户代码(如AB001)、地址和电话号码等信息。所以在其他方面,我必须使用字符串。我的问题是:有没有其他方法来编写代码,或者有没有其他方法来解决这个问题 这是代码的样子:C# 声明具有相似签名和参数的方法,c#,methods,overloading,C#,Methods,Overloading,因此,基本上我被要求编写代码,但正如标题所说,我在添加具有相同签名的多个字符串方法时遇到了困难。我必须创建一个类,在其中存储客户名称、客户代码(如AB001)、地址和电话号码等信息。所以在其他方面,我必须使用字符串。我的问题是:有没有其他方法来编写代码,或者有没有其他方法来解决这个问题 这是代码的样子: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syste
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”的成员 错误2:类型“myapp2.Customer”已定义名为 具有相同参数类型的“客户” 错误3:类型“myapp2.Customer”已定义名为 具有相同参数类型的“客户” 我在使用C#方面不是很有经验,所以如果有人能帮忙,请帮忙 使用: 属性是提供灵活机制来读取、写入或计算私有字段值的成员 或: 使用: 属性是提供灵活机制来读取、写入或计算私有字段值的成员 或:
您可能应该使用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方法,考虑使用属性而不是方法对:
在这种情况下,您将能够创建如下类:var customer = new Customer {
NameOfAcc = "John Doe",
Phone = "123 456 7890"
};
您可能应该使用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方法,考虑使用属性而不是方法对:
在这种情况下,您将能够创建如下类:var customer = new Customer {
NameOfAcc = "John Doe",
Phone = "123 456 7890"
};
错误1:类型“myapp2.Customer”已使用相同的参数类型定义了名为“Customer”的成员错误2:类型“myapp2.Customer”已使用相同的参数类型定义了名为“Customer”的成员错误3:类型“myapp2.Customer”已定义了名为具有相同参数类型的“Customer”这些是我收到的错误..错误1:类型“myapp2.Customer”已使用相同参数类型定义了名为“Customer”的成员错误2:类型“myapp2.Customer”已使用相同参数类型定义了名为“Customer”的成员参数类型错误3:类型“myapp2.Customer”已使用相同的参数类型定义了名为“Customer”的成员这些是我收到的错误。。