[英]C# abstract class with abstract property of Generic List override
I am trying to use some base Abstract classes for a Transaction, Order and an OrderCollection. 我正在尝试对Transaction,Order和OrderCollection使用一些基本Abstract类。 TransactionCog inherits Transaction and OrderCollection inherits List and OrderCogCollection inherits OrderCollection and OrderCog inherits Order. TransactionCog继承Transaction,OrderCollection继承List,OrderCogCollection继承OrderCollection,OrderCog继承Order。 The TransactionCog must have a member of OrderCogCollection. TransactionCog必须具有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. 我试图使用Transaction具有OrderCollection的抽象属性,以便TransactionCog必须重写该属性,但改用OrderCogCollection的类型。 Of course the way I'm going right now is giving me a TransactionCog does not implement inherited abstract member. 当然,我现在要走的方式是给我一个TransactionCog,它不实现继承的抽象成员。
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. 我正在尝试这样做,以便如果要下一个要订购的Widget或Gear,我将从基础继承,这迫使我用相应的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. 造成这种情况的部分原因是,每个Order'ItemName'Collection都会拥有一些自己的特殊功能和属性,而其他的则不会。 I'm just having trouble seeing where I should be going to have this ability 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; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.