子类竟然不继承父类的构造方法
本帖最后由 wangxiangtan2 于 2023-7-6 16:13 编辑今天才知道……
想弄个泛型集合,继承这个货:
public class BlockingCollection<T> : IEnumerable<T>, ICollection, IEnumerable, IDisposable
{
public BlockingCollection();
public BlockingCollection(int boundedCapacity);
public BlockingCollection(IProducerConsumerCollection<T> collection);
public BlockingCollection(IProducerConsumerCollection<T> collection, int boundedCapacity);
public int BoundedCapacity { get; }
public int Count { get; }
public bool IsAddingCompleted { get; }
public bool IsCompleted { get; }
……
}
然后发现直接继承一下是不行的,必须显式继承父类构造函数:
public class MsgBlockCollection<T>:BlockingCollection<T>
{
private MsgBlockCollection<T> instance;
public MsgBlockCollection() : base() { }
public MsgBlockCollection(int i) : base(i) { }
public MsgBlockCollection(IProducerConsumerCollection<T> collection) : base(collection) { }
public MsgBlockCollection(IProducerConsumerCollection<T> collection, int i) : base(collection,i) { }
public MsgBlockCollection<T> Instance
{
get
{
if (instance != null)
{
return instance;
}
else
{
return new MsgBlockCollection<T>(new ConcurrentQueue<T>());
}
}
}
}
主要是想要这个线程安全同时实现先进先出、先进后出的集合:
//先进先出(FIFO)
BlockingCollection<int> bc = new BlockingCollection<int>(new ConcurrentQueue<int>());
//先进后出(LIFO)
BlockingCollection<int> bc2 = new BlockingCollection<int>(new ConcurrentStack<int>());
页:
[1]