2013-07-25 66 views
-4

我目前正在做一些课程编码,并想知道我的项目出了什么问题?不包含带2个参数的构造函数吗?

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);” theres error says不包含带有2个参数的构造函数我可以知道为什么吗?

+4

从Java来了,对不对?请使用[real Properties](http://msdn.microsoft.com/en-us/library/x9fsa0sw.aspx)而不是'getABC()'和'setABC()'方法。 –

+0

要详细说明@HighCore所说的,而不是getABC()和setABC,请使用公共字符串YourString {get {return _yourString} set {_yourString = value}}; –

+0

然后你可以使用属性初始值设定项:'telNo = new ContactNo(){ContactType =“Mobile No:”,Number = 95656565};' –

回答

6

这将是因为您没有在ContactNo类中包含两个参数的构造函数,如错误所示。在课堂上看看,你会注意到那里没有构造函数。不过,您在ContactPerson类中有一个。

此行:telNo = new ContactNo("Mobile No: ", 95656565); 正在调用ContactNo的构造函数,该函数接受两个参数:一个字符串和一个int。您目前没有设置为执行此操作的构造函数,这就是您的错误所在。您可以通过添加

public ContactNo(string s, int n){ 
    //initializations 
} 

或其他性质的东西。或者,如果您使用字符串作为数字(它看起来像),请将int n替换为string s2或您希望调用它的任何内容。

1

因为你没有联系没有2参数的构造函数。我猜你是使用具有2个参数

public ContactPerson(string in_Name, ContactNo in_No) 

从你的代码,它看起来像你必须把它添加到您的类ContactNo

public ContactNo(string type, string umber) 
{ 
    contactType = type; 
    contactNo = number; 
} 
0
public ContactNo(string type, string umber) 
{ 
     contactType = type; 
     contactNo = number; 
} 

添加这是你的其他类混淆它在你的联系人类中。

你得到错误的原因是因为没有两个参数的构造函数。

0

以下内容添加到您的ContactNo类:

public ContactNo(string inType, string inNo) 
{ 
    contactType = inType; 
    contactNo = inNo; 
} 
0

您不必构造带2个参数。添加这种构造在ContactNo类

public ContactNo(string contactType, string contactNo) 
{ 
    this.contactType = contactType; 
    this.contactNo = contactNo; 
} 
0

您需要声明的构造为您ContactNo类。类只提供了一个没有参数的默认构造函数。

你需要的构造函数如下:

public ContactNo(string contactType, string contactNo) { 
    this.contactType = contactType; 
    this.contactNo = contactNo; 
} 
0

既然你传递一个stringint我想你想创建一个新的ContactPerson,不ContactNo。然而,如果你真的想要ContactNo,要么添加构造函数:

class ContactNo { public string ContactType {get;组; } public string ContactNo {get;组; }

public ContactNo(string type, string no) 
{ 
    ContactType = type; 
    ContactNo = no; 
} 

}

或(与属性)初始化这样的:

ContactNo contact = { ContactType = "The type", ContactNo = "The No" }; 
相关问题