[英]Trying to avoid multiple sub-types in a class structure
我有兩個抽象類Business&Person。 問題是我有一個可以是商業或個人的客戶類型。 有沒有辦法對此進行建模,以便我沒有CustomerBusiness和CustomerPerson類? 多重繼承不是一種選擇,因為這是C#。 我一直在看這個東西,以至於看不到樹木的森林。
public abstract class Business {
public string Name { get; set; }
}
public abstract class Person {
public string FirstName { get; set; }
public string LastName { get; set; }
public string MiddleName { get; set; }
public DateTime? BirthDate { get; set; }
public string Comments { get; set; }
}
public class CustomerBusiness : Business, CustomerRoles {
public bool BillTo { get; set; }
public bool ShipTo { get; set; }
public bool DeliverTo { get; set; }
public EntityType Type { get; set; }
}
public class CustomerPerson : Person, CustomerRoles {
public bool BillTo { get; set; }
public bool ShipTo { get; set; }
public bool DeliverTo { get; set; }
public EntityType Type { get; set; }
}
public interface CustomerRoles {
bool BillTo { get; set; }
bool ShipTo { get; set; }
bool DeliverTo { get; set; }
}
您可能更喜歡使用Business
和Person
類“擁有” ICustomerRoles
而不是“是” ICustomerRoles
。
public class Business
{
public string Name { get; set; }
public ICustomerRoles CustomerRoles { get; set; }
}
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string MiddleName { get; set; }
public DateTime? BirthDate { get; set; }
public string Comments { get; set; }
public ICustomerRoles CustomerRoles { get; set; }
}
我要說的是,您的繼承制度順序錯誤。 客戶應該是基類,然后業務和人員應該是客戶類的實現。 我確信除了CustomerRoles界面中的客戶之外,所有類型的客戶都有更多的客戶屬性。
我原以為會有類似CustomerCode等的東西。另外你可以說所有客戶都有一個Name屬性,並且由每個子類中的getter決定是否返回適當的值。 因為在Business中有一個名為Name的屬性,而一個人將擁有FirstName,MiddleName,Surname和Name屬性,它們將以某種方式連接這些屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.