如何使用泛型而不是继承来拥有某些内容的多个版本



在您的公司中,假设您有以下代码:

public abstract Phone
{
    public int PhoneID {get;set;}
    public string PhoneNumber {get;set;}
}
public CustomerPhone : Phone
{
    public int CustomerID {get;set;}
}
public AccountPhone : Phone
{
    public int AccountID {get;set;}
}

这应该意味着我们有多种类型的电话,有些是客户电话,有些是帐户电话,等等......

问题是"这是否可能,如果是,那么如何?似乎最简单的方法是拥有一个通用的 Phone 类,您可以在其中插入一个类型,然后它只会使用该类型的信息(帐户 ID 或客户 ID)在需要时使用。我还在检查这是否可以在没有 DI 的情况下(无论是通过构造函数、方法还是属性)。

我脑子里的东西看起来像这样:

public interface IUsePhone
{
    int GetOwnerID();
}
public class Phone<T> where T : IUsePhone
{
    //all of Phone's properties from above.
    public int GetOwnerID()
    {
        //return T or item or something's GetOwnerID();
    }
}
public class Account : IUsePhone
{
    private int _accountID;
    //other Account members, including an AccountID property.
    public int GetOwnerID()
    {
        return _accountID;
    }   
    public Phone<Account> Phone { get; set; }
}
public class Customer : IUsePhone
{
    private int _customerID;
    //other Customer members, including an CustomerID property.
    public int GetOwnerID()
    {
        return _customerID;
    }
    public Phone<Customer> Phone { get; set; }
}

这不会编译,因为Phone的GetOwnerID()目前没有任何方法可以返回其所有者的GetOwnerID()结果。我希望从客户的角度来看,最终结果可以看起来像这样:

Account myAccount = new Account();
myAccount.AccountID = 10;
int ownerID = myAccount.Phone.GetOwnerID(); //this would return 10.

我认为你需要问问自己为什么要这样做。

如果你真的想要一堆不同的类型,所有这些类型都满足了Phone契约,你最好有一个接口,再加上一个抽象的基本实现:

public interface IPhone
{
    int PhoneID {get;set;}
    string PhoneNumber {get;set;}
}
public abstract AbstractPhoneBase : IPhone
{
    public int PhoneID {get;set;}
    public string PhoneNumber {get;set;}
}
public CustomerPhone : AbstractPhoneBase
{
    public int CustomerID {get;set;}
}
我认为

您的示例很好 - 只是缺少接受实现 IUsePhone(帐户、客户等)的所有者实例的构造函数。

尝试将其添加到您的Phone<T>类中。

    public IUsePhone Owner { get; private set; }
    public Phone(T owner)
    {
        this.Owner = owner;
    }
    public int GetOwnerID()
    {
        return this.Owner.GetOwnerID();
    }

注意:在您的示例中,不要忘记必须先设置 Phone 属性,然后才能调用myAccount.Phone.GetOwnerID();

如果你这样做,我会沿着已经建议的抽象基类路线,并在基方法中设置电话,如下所示:

public virtual void SetPhoneNumber<T>(string number)
    {
        this.Phone = new Phone<T>(this);
        this.Phone.Number = number;
    }

因此,您的使用最终将如下所示:

    Account myAccount = new Account();
    myAccount.AccountID = 10;
    myAccount.SetPhoneNumber("123456");
    int ownerID = myAccount.Phone.GetOwnerID(); // this would return 10.

相关内容

最新更新