我有以下类(POCO 的?),其中第二个应该扩展第一个。
我猜我需要一个引用基本实例的属性,在本例中为 Business,但我是否还需要一个构造函数来“填充”该属性,因为没有 Business 保险公司就不可能存在?
我对此的引用是基础数据库,其中 tblInsuranceCompany
在 BusinessID
列上有一个返回 tblBusiness
的 FK 约束。
此外,我对 OOP 非常陌生,所以请指出您可能认为“错误”的任何其他内容。谢谢,
public class Business
{
public int BusinessID { get; set; }
public BusinessType BusinessType { get; set; }
public string Name { get; set; }
public string ContactName { get; set; }
public string EmailAddress { get; set; }
public DateTime? InactiveDate { get; set; }
public IList<Address> Addresses { get; set; }
public IList<Phone> Phones { get; set; }
public string DisplayString { get { return this.ToString(); } }
public override string ToString()
{
return String.Format("{0}: {1}", Name, BusinessType.TypeDescription);
}
}
public class InsuranceCompany : Business
{
public int InsuranceCompanyID { get; set; }
public Business Business { get; set; }
public InsuranceCompanyType InsuranceCompanyType { get; set; }
public string DRIInsuranceCompanyNumber { get; set; }
public string DisplayString { get { return this.ToString(); } }
public override string ToString()
{
return String.Format("{0}: {1}", Business.Name, InsuranceCompanyType.TypeDescription);
}
}
最佳答案
I am guessing I need a property referencing the base instance, Business in this case,
不,你不需要这个。 InsuranceCompany 是企业。它还将具有 Business
的所有属性,因为它继承自 Business
。您不需要属性来引用它,因为对 InsuranceCompany
对象的引用将与对“业务”部分的引用相同(它们是同一个对象)。
but do I also need a Constructor to "fill" that property since an Insurance Company can't exist without a Business?
所有类都有一个构造函数 - 当您构造一个 InsuranceCompany 时,它也会有效地构造“业务”。
话虽如此,您可能需要一个将相关信息传递给基类构造函数的构造函数 - 但如果您不提供,编译器将为您创建一个默认构造函数。
关于c# - 我需要一个具有继承性的构造函数吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9267512/