c# 多基 类
c# Multiple base classes
我在设计 classes 时遇到了一点问题。
我想做的是:
有 4 个 classes,每个都有自己的属性。实际地址和邮寄地址可以是国内地址或国际地址。联系人 class 应该有两个某种类型的属性,我可以在其中访问所有需要的属性。
我已经尝试为 national/international 和 physical/mailing 创建基础 classes,但我正在为它们都有不同的属性而苦苦挣扎。
您将如何以适当的方式为 classes 建模?在 c# 中甚至可能吗?恐怕我只需要为 4 个 classes 中的每一个在 Contact 上创建 4 个属性,然后进行 null 检查以查看对象具有哪种类型的地址。
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; }
}
此致,
代码错误
第 1 部分
这次采用不同的方法 --> 我重构了您的原始属性/class有点。
AddressRule 与单独的属性
为什么 InternationalAddress 没有 Street、HouseNumber 等?我将其重构为一个列表,其中可以包含一个国家地址条目和多个国际地址条目。
验证属性
为什么物理/邮寄地址中有这些验证属性?据我所知,这些应该是最高级别的。
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 部分
如果您确实需要单独的 class 用于国际和国内,我建议在您的物理/电子邮件 class 中创建一个 属性 类型的 AddressBase。此 AddressBase 属性 可以是国际或国内。
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; }
}
我在设计 classes 时遇到了一点问题。
我想做的是: 有 4 个 classes,每个都有自己的属性。实际地址和邮寄地址可以是国内地址或国际地址。联系人 class 应该有两个某种类型的属性,我可以在其中访问所有需要的属性。
我已经尝试为 national/international 和 physical/mailing 创建基础 classes,但我正在为它们都有不同的属性而苦苦挣扎。
您将如何以适当的方式为 classes 建模?在 c# 中甚至可能吗?恐怕我只需要为 4 个 classes 中的每一个在 Contact 上创建 4 个属性,然后进行 null 检查以查看对象具有哪种类型的地址。
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; }
}
此致,
代码错误
第 1 部分
这次采用不同的方法 --> 我重构了您的原始属性/class有点。
AddressRule 与单独的属性
为什么 InternationalAddress 没有 Street、HouseNumber 等?我将其重构为一个列表,其中可以包含一个国家地址条目和多个国际地址条目。
验证属性
为什么物理/邮寄地址中有这些验证属性?据我所知,这些应该是最高级别的。
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 部分
如果您确实需要单独的 class 用于国际和国内,我建议在您的物理/电子邮件 class 中创建一个 属性 类型的 AddressBase。此 AddressBase 属性 可以是国际或国内。
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; }
}