c#多个基类

时间:2016-01-14 15:47:17

标签: c# oop object-oriented-analysis ooad

我在设计课程时遇到了一些问题。

我想做的是以下内容: 每个属性有4个类。物理和邮寄地址可以是国内的或国际的。联系人类应该有两种属性,我可以访问所有需要的属性。

我已经尝试过为国家/国际和物理/邮寄创建基类,但我对它们都具有不同属性的事实感到困惑。

您如何以适当的方式为课程建模?它甚至可能在c#中?我担心我只需要为4个类中的每个类在Contact上创建4个属性,并进行空检查以查看该对象具有哪种类型的地址。

class Program
{
    static void Main(string[] args)
    {
        Contact c = new Contact();
    }
}

public class Contact
{
    public xxx PhysicalAddress { get; set; }
    public xxx MailingAddress { get; set; }
}

public class NationalAddress
{
    public bool IsDeleted { get; set; }
    public DateTime CreationDate { get; set; }

    public Country Country { get; set; }
    public PhoneNumber Landline { get; set; }

    public string Street{ get; set; }
    public string HouseNumber{ get; set; }
    public string PostalCode{ get; set; }
    public string City{ get; set; }
}

public class InternationalAddress
{
    public bool IsDeleted { get; set; } 
    public DateTime CreationDate { get; set; }

    public Country Country { get; set; }
    public PhoneNumber Landline { get; set; }

    public string AdresRule1 { get; set; }
    public string AdresRule2 { get; set; }
    public string AdresRule3 { get; set; }
}

public class PhysicalAddress
{        
    public bool IsDeleted { get; set; }
    public DateTime CreationDate { get; set; }

    public bool IsVerified { get; set; }
    public DateTime ValidFrom { get; set; }
    public DateTime? ValidTo { get; set; }

    //Semi-detached/Terraced/Appartment/...
    public TypeOfBuilding Building{ get; set; } 
    public bool Occupied { get; set; } 
}

public class MailingAddress
{
    public bool IsDeleted { get; set; }
    public DateTime CreationDate { get; set; }

    public bool IsVerified { get; set; }
    public DateTime ValidFrom { get; set; }
    public DateTime? ValidTo { get; set; }

    public bool AllowCommercialPress { get; set; }
    public bool AllowOfficialPress { get; set; }
}

此致

Miscode

1 个答案:

答案 0 :(得分:2)

第1部分

这次采用不同的方法 - >我稍微重构了你原来的属性/类。

AddressRule与单独的属性

为什么国际地址没有Street,HouseNumber,等等?我将其重构为一个List,它可以为您的NationalAddress提供一个条目,为您的InternationalAddress提供多个条目。

验证属性

为什么物理/邮寄地址中的这些验证属性?据我所知,这些应该是最高级别的。

public class Contact
{
    public PhysicalAddress PhysicalAddress { get; set; }
    public MailingAddress MailingAddress { get; set; }
}

public class AddressRule
{
    public string Street { get; set; }
    public string HouseNumber { get; set; }
    public string PostalCode { get; set; }
    public string City { get; set; }
}

public class BaseAddress
{
    public bool IsDeleted { get; set; }
    public DateTime CreationDate { get; set; }

    public Country Country { get; set; }
    public PhoneNumber Landline { get; set; }

    public List<AddressRule> AdressRules { get; set; }

    public bool IsVerified { get; set; }
    public DateTime ValidFrom { get; set; }
    public DateTime? ValidTo { get; set; }
}

public class PhysicalAddress : BaseAddress
{        
    //Semi-detached/Terraced/Appartment/...
    public TypeOfBuilding Building { get; set; }
    public bool Occupied { get; set; }
}

public class MailingAddress : BaseAddress
{       
    public bool AllowCommercialPress { get; set; }
    public bool AllowOfficialPress { get; set; }
}

第2部分

如果你真的需要一个单独的国际和国家级我建议在你的Physical / Email类中创建一个AddressBase类型的属性。此AddressBase属性可以是International或National。

public class Contact
{
    public PhysicalAddress PhysicalAddress { get; set; }
    public MailingAddress MailingAddress { get; set; }
}

public class AddressBase
{
    public bool IsDeleted { get; set; }
    public DateTime CreationDate { get; set; }
    public Country Country { get; set; }
    public PhoneNumber Landline { get; set; }
}

public class NationalAddress : AddressBase
{
    public string Street { get; set; }
    public string HouseNumber { get; set; }
    public string PostalCode { get; set; }
    public string City { get; set; }
}

public class InternationalAddress : AddressBase
{
    public string AdresRule1 { get; set; }
    public string AdresRule2 { get; set; }
    public string AdresRule3 { get; set; }
}


//I'm guessing Mailing and Physical is meant to know where to ship to, hence the "Transport" prefix.
public class TransportAddressBase
{
    **public AddressBase AddressBaseInformation{ get; set; }**

    public bool IsVerified { get; set; }
    public DateTime ValidFrom { get; set; }
    public DateTime? ValidTo { get; set; }
}



public class PhysicalAddress : TransportAddressBase
{       
    //Semi-detached/Terraced/Appartment/...
    public TypeOfBuilding Building { get; set; }
    public bool Occupied { get; set; }
}

public class MailingAddress : TransportAddressBase
{       
    public bool AllowCommercialPress { get; set; }
    public bool AllowOfficialPress { get; set; }
}