避免使用只有很少不同属性的多个类

时间:2017-02-05 19:36:10

标签: c# winforms class inheritance

除了一个变量类型外,我有两个接近相同的类。
以下示例应该说明问题。

class Customer
{
    byte Reputation;
    const byte MaximumReputation = 255;
    int Data;
    string MoreData;
    Briefcase EvenMoreData;

    public Customer(byte reputation, int data, string moreData, Briefcase evenMoreData)
    {
        Reputation = reputation;
        Data = data;
        MoreData = moreData;
        EvenMoreData = evenMoreData;
    }

    public float Level()
    {
        return Reputation / MaximumReputation;
    }

    public string Information()
    {
        return Data.ToString() + ", " + MoreData + EvenMoreData.Id;
    }
}
class CustomerDeluxe
{
    ushort Reputation;
    const byte MaximumReputation = 255;
    int Data;
    string MoreData;
    Briefcase EvenMoreData;

    public CustomerDeluxe(ushort reputation, int data, string moreData, Briefcase evenMoreData)
    {
        Reputation = reputation;
        Data = data;
        MoreData = moreData;
        EvenMoreData = evenMoreData;
    }

    public float Level()
    {
        return Reputation / MaximumReputation;
    }

    public string Information()
    {
        return Data.ToString() + ", " + MoreData + EvenMoreData.Id;
    }
}

由于Customer的声誉不会超过255,我可以存储在一个字节中。在我的应用程序中,此字段是一个大型数组。这就是为什么我想选择最小的类型。但是对于少数情况,字段可以超过一个字节的情况下必须有另一个类。

我怎样才能"合并"这两个类?

2 个答案:

答案 0 :(得分:2)

使用泛型:

public class BaseCustomer<TRep>
{
    protected TRep Reputation;
}

public class CustomerDeluxe : BaseCustomer<ushort>
{
}

public class Customer : BaseCustomer<byte>
{
}

使用泛型,你基本上是在说:&#34;我不确定TRep的类型是什么,但它会表现出来。&#34;如果在BaseCustomer<TRep>中如上所述,则称泛型为 open 。在我们使用它之前,我们需要关闭它。当我们继承它时,然后wev 关闭使用特定类型编辑它:在一个实例中我们将其指定为ushort,在另一个实例中我们将其指定为byte。< / p>

通过一些重构,您可以将大部分代码推送到BaseCustomer<T>

public abstract class BaseCustomer<TRep>
{
    protected TRep Reputation;
    protected const byte MaximumReputation = 255;
    int Data;
    string MoreData;
    Briefcase EvenMoreData;

    public BaseCustomer(TRep reputation, int data, string moreData, Briefcase evenMoreData)
    {
        Reputation = reputation;
        Data = data;
        MoreData = moreData;
        EvenMoreData = evenMoreData;
    }

    public abstract float Level();


    public string Information()
    {
        return Data.ToString() + ", " + MoreData + EvenMoreData.Id;
    }
}

public class CustomerDeluxe : BaseCustomer<ushort>
{
    public CustomerDeluxe(ushort reputation, int data, string moreData, Briefcase evenMoreData) 
        : base(reputation, data, moreData, evenMoreData)
    {
    }

    public override float Level()
    {
        return Reputation / MaximumReputation;
    }
}

public class Customer : BaseCustomer<byte>
{
    public Customer(byte reputation, int data, string moreData, Briefcase evenMoreData) 
        : base(reputation, data, moreData, evenMoreData)
    {
    }

    public override float Level()
    {
        return Reputation / MaximumReputation;
    }
}

答案 1 :(得分:1)

class CustomerDeluxe : CustomerBase
{
    ushort Reputation;

    public override ushort GetReputation()
    {
        return Reputation;
    }
}

class Customer : CustomerBase
{
    byte Reputation;
    public override ushort GetReputation()
    {
        return Reputation;
    }
}

abstract class CustomerBase
{
    const byte MaximumReputation = 255;
    public abstract ushort GetReputation();

    public float Level()
    {
        return GetReputation()/MaximumReputation;
    }
}