如何使用泛型而不是继承来拥有多个版本的东西

时间:2012-11-28 21:13:09

标签: c# .net generics inheritance

在贵公司,请说您有以下代码:

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;}
}

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

问题是“这是可能的,如果是,那么如何?”看起来最简单的方法就是拥有一个可以插入Type的通用Phone类,然后只需要使用Type的信息(AccountID或CustomerID)即可。我也在检查这是否可能没有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.

2 个答案:

答案 0 :(得分:4)

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

如果你真的想要一堆不同的类型,所有这些类型都符合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;}
}

答案 1 :(得分:0)

我认为您的示例很好 - 只是错过了实现IUsePhone(帐户,客户等)的所有者实例的构造函数。

尝试将此添加到您的Phone<T>课程。

    public IUsePhone Owner { get; private set; }

    public Phone(T owner)
    {
        this.Owner = owner;
    }

    public int GetOwnerID()
    {
        return this.Owner.GetOwnerID();
    }

注意:在您的示例中,不要忘记您必须在调用myAccount.Phone.GetOwnerID();之前设置Phone属性。

如果你这样做,我会沿着已经建议的抽象基类路线走下去,并按照以下方式在基本方法中设置Phone:

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.