在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