在C#中,我有一個AddressBook類,並且在該類中有一個聯繫人列表。我有一個Contact類繼承自我的抽象Person類(它具有Name,Sex,DOB)。但我希望每個聯繫人都能夠獲得聯繫信息,所以我創建了另一個名爲ContactInfo的類(電話號碼,地址,城市)。我在解決如何將ContactInfo屬性(數字,地址等)附加到用戶決定輸入到AddressBook的每個聯繫人時遇到問題。下面是我的聯繫人類和我的ContactInfo類:我在向類中添加信息時遇到問題
public class Contact : Person
{
public ContactInfo info, newInfo;
public Contact()
{ }
public ContactInfo GetContactInfo()
{
var info = new ContactInfo();
return info.GatherContactInfo();
}
//public ContactInfo Info { get; set; }
}
public class ContactInfo
{
public string PhoneNumber { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string State { get; set;}
public Contact contact;
public ContactInfo()
{ }
public ContactInfo GatherContactInfo()
{
var newInfo = new ContactInfo();
Console.WriteLine("Enter their phone number:");
string phoneNumber = Console.ReadLine();
newInfo.PhoneNumber = StorePhoneNumber(phoneNumber);
Console.WriteLine("Enter their address: ");
string address = Console.ReadLine();
newInfo.Address = StoreAddress(address);
Console.WriteLine("Enter city: ");
string city = Console.ReadLine();
newInfo.City = StoreCity(city);
Console.WriteLine("Enter State: ");
string _state = Console.ReadLine();
newInfo.State = StoreState(_state);
return newInfo;
}
'我有問題'是什麼問題?你無法弄清楚如何以一種合理的方式來設計它,或者你的實現在這裏不起作用?你可以說得更詳細點嗎? – tnw 2013-05-07 20:05:45
您通常希望將模型與數據條目分開。因此,用戶輸入聯繫人詳細信息,並使用輸入的字段創建或填寫ContactInfo。 – flup 2013-05-07 20:08:37
是否有其他信息不直接存儲在'聯繫人'中的原因? ...或者是那個問題?即,*如何將字段添加到子類?*(如果是這樣,答案與其他類相同*) – svidgen 2013-05-07 20:09:56