开发者

C# abstract class with abstract property of Generic List override

开发者 https://www.devze.com 2023-04-11 04:39 出处:网络
I am trying to use some base Abstract classes for a Transaction, Order and an OrderCollection.TransactionCog inherits Transaction and OrderCollection inherits List and OrderCogCollection inherits Orde

I am trying to use some base Abstract classes for a Transaction, Order and an OrderCollection. TransactionCog inherits Transaction and OrderCollection inherits List and OrderCogCollection inherits OrderCollection and OrderCog inherits Order. The TransactionCog must have a member of OrderCogCollection.

I am trying to use Transaction to have an abstract Property of OrderCollection so that TransactionCog must override the property but use a type of OrderCogCollection instead. Of course the way I'm going right now is giving me a TransactionCog does not implement inherited abstract member.

I'm trying this so that if I have a Widget or Gear to order next, I inherit from the bases and it forces me to override the property with corresponding Order'ItemName'Collection. Part of the reason for this is each Order'ItemName'Collection will have some of it's own special functions and properties that others will not. I'm just having trouble seeing where I should be going to have this abili开发者_如何转开发ty work if it's possible.

public abstract class Transaction
{
    public abstract OrderCollection<Order> Orders
    { get; set; }
}

public abstract class Order
{
    private int _id;

    public int ID
    {
        get { return _id; }
        set { _id = value; }
    }
}

public abstract class OrderCollection<T> : System.Collections.Generic.List<T>
{
    abstract public int GetIntValue(int test);
}

class OrderCog : TestWindowsFormCsharp.Classes.BaseClasses.Order
{
    public OrderCog()
    {
    }
}

public class OrderCogCollection<OrderCog> : TestWindowsFormCsharp.Classes.BaseClasses.OrderCollection<OrderCog> 
{
    public OrderCogCollection()
    {
    }

    public override int GetIntValue(int test)
    {
        return test;
    }
}

class TransactionCog : TestWindowsFormCsharp.Classes.BaseClasses.Transaction
{
    private OrderCogCollection<OrderCog> cogs;

    public TransactionCog()
    {
    }

    public override OrderCogCollection<OrderCog> Orders
    {
        get { return cogs; }
        set { cogs = value; }
    }

}


Override must keep the declaration signature to be polymorphic, so in your case:

public override OrderCollection<Order> Orders
{
    get { return cogs; }
    set 
    {
        // assert value is OrderCogCollection<OrderCog>  
        cogs = (OrderCogCollection<OrderCog>)value; 
    }
}

public OrderCogCollection<OrderCog> OrderCogs
{
    get { return cogs; }
    set { cogs = value; }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号