我有一堂课:
public class Person
{
public string FirstName { get; private set; }
public string LastName { get; private set; }
public string Email { get; private set; }
public string Telephone { get; private set; }
public Address Address { get; private set; }
public Person(string firstName, string lastName)
{
//do null-checks
FirstName = firstName;
LastName = lastName;
Address = new Address();
}
public void AddOrChangeEmail(string email)
{
//Check if e-mail is a valid e-mail here
Email = email;
}
public void AddOrChangeTelephone(string telephone)
{
//Check if thelephone has correct format and valid symbols
Telephone = telephone;
}
public void AddOrChangeAdress(Address address)
{
Address = address;
}
不在构造函数中的属性是可选的,即此人不需要电子邮件、地址或电话。但是,我想给类的用户一个创建对象的机会,而不必首先提供所需的信息,然后必须找出使用什么方法来添加信息。
问题:
- 创建 3 个额外的重载给他们这个选项是否正确?
- 我应该允许可选属性上的公共设置器并在那里进行验证吗?
- 如果此人结婚并更改姓氏,我是否需要其他方法来更改姓氏,或者我是否也应该将此设置器公开,并且只在构造函数中要求它们?