c# - 我需要一个具有继承性的构造函数吗?

标签 c# .net oop inheritance

我有以下类(POCO 的?),其中第二个应该扩展第一个。

我猜我需要一个引用基本实例的属性,在本例中为 Business,但我是否还需要一个构造函数来“填充”该属性,因为没有 Business 保险公司就不可能存在?

我对此的引用是基础数据库,其中 tblInsuranceCompanyBusinessID 列上有一个返回 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/

相关文章:

c# - 底层连接已关闭 : The server committed a protocol violation. FTP

c# - 如何从 Windows8 商店应用程序中的字节数组获取流

.net - 将wsHttpBinding SSL传输安全性与消息安全性结合使用有什么好处?

.net - 使用 .Net 进行文本挖掘、事实提取、语义分析

Java:何时将方法设为静态 v. 实例

php - PHP中的依赖倒置原则

c# - 编码的 UI 测试和 MessageBox 出现问题 - 搜索条件

c# - ASP.Net Video 中的 DataList 应在点击时放大

java - 如何以静态和非静态方式使用枚举

c# - 在 C# 中否定浮点值的最佳方法