不包含构造函数,需要两个参数? [英] Does not contain a constructor that takes 2 arguments?
问题描述
我目前在做一些类编码,不知道什么地方出了错与我的项目?
I am currently doing some class coding and wonder what went wrong with my project?
class ContactPerson
{
string name;
ContactNo telNo;
public ContactPerson(string in_Name, ContactNo in_No)
{
name = in_Name;
telNo = new ContactNo();
}
public string getName()
{
return name;
}
public ContactNo getContactInfo()
{
return telNo;
}
public void setName(string in_Name)
{
name = in_Name;
}
public void setContactInfo (ContactNo in_No)
{
telNo = in_No;
}
}
}
}
class ContactNo
{
string contactType;
string contactNo;
public void setContactType(string in_Type)
{
contactType = in_Type;
}
public string getContactType()
{
return contactType;
}
public void setContactNo(string in_No)
{
contactNo = in_No;
}
public string getContactNo()
{
return contactNo;
}
}
}
}
class Program
{
static void Main(string[] args)
{
ContactNo telNo;
telNo = new ContactNo("Mobile No: ", 95656565);
ContactPerson myFriend;
myFriend = new ContactPerson("Fred Smith", telNo);
string strName;
strName = myFriend.getName();
Console.WriteLine(" " + strName);
ContactNo outContact;
outContact = myFriend.getContactInfo();
outContact.getContactType();
Console.WriteLine(outContact);
outContact.getContactNo();
Console.WriteLine(outContact);
Console.ReadLine();
}
}
}
}
在程序类
telNo =新ContactNo(手机号码:95656565);
孤单的错误说法不包含一个构造函数2参数
可我知道为什么吗?
At the program class " telNo = new ContactNo("Mobile No: ", 95656565); " theres error saying Does not contain a constructor that takes 2 arguments may i know why?
推荐答案
这可能是因为你没有包含在这两个参数的构造函数ContactNo类,如错误提示。您可以在类一看,你会发现,没有构造存在。 。你有一个在ContactPerson类,虽然
That would be because you don't have a constructor that contains two arguments in the ContactNo class, as the error suggests. Take a look in the class, and you'll notice that there is no constructor there. You DO have one in the ContactPerson class, though.
这行: telNo =新ContactNo(手机号码:95656565);
呼吁ContactNo采用两个参数的构造函数:一个字符串,一个int。您没有被设置为当前做一个构造函数,而这也正是你的错误。你可以通过添加
This line: telNo = new ContactNo("Mobile No: ", 95656565);
is calling a constructor for ContactNo that takes two arguments: a string, and an int. You don't have a constructor that is set up to do this currently, and that's where your error is. You could create one by adding
public ContactNo(string s, int n){
//initializations
}
或类似这种事情。或者,如果您使用的是数字的字符串(它看起来像),取代 INTñ
与字符串s2
或任何你想调用它。
or something of that nature. Or, if you're using a string for the number (which it looks like), replace int n
with string s2
or whatever you wish to call it.
这篇关于不包含构造函数,需要两个参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!