鱼C论坛

 找回密码
 立即注册
查看: 2186|回复: 0

[学习笔记] 子类竟然不继承父类的构造方法

[复制链接]
发表于 2023-7-6 16:12:06 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 wangxiangtan2 于 2023-7-6 16:13 编辑

今天才知道……
想弄个泛型集合,继承这个货:
  1. public class BlockingCollection<T> : IEnumerable<T>, ICollection, IEnumerable, IDisposable
  2.     {
  3.         public BlockingCollection();
  4.         public BlockingCollection(int boundedCapacity);
  5.         public BlockingCollection(IProducerConsumerCollection<T> collection);
  6.         public BlockingCollection(IProducerConsumerCollection<T> collection, int boundedCapacity);

  7.         public int BoundedCapacity { get; }
  8.         public int Count { get; }
  9.         public bool IsAddingCompleted { get; }
  10.         public bool IsCompleted { get; }
  11. ……
  12. }
复制代码


然后发现直接继承一下是不行的,必须显式继承父类构造函数:
  1. public class MsgBlockCollection<T>:BlockingCollection<T>
  2.     {
  3.         private MsgBlockCollection<T> instance;

  4.         public MsgBlockCollection() : base() { }

  5.         public MsgBlockCollection(int i) : base(i) { }

  6.         public MsgBlockCollection(IProducerConsumerCollection<T> collection) : base(collection) { }
  7.         public MsgBlockCollection(IProducerConsumerCollection<T> collection, int i) : base(collection,i) { }
  8.         public MsgBlockCollection<T> Instance
  9.         {
  10.             get
  11.             {
  12.                 if (instance != null)
  13.                 {
  14.                     return instance;
  15.                 }
  16.                 else
  17.                 {
  18.                     return new MsgBlockCollection<T>(new ConcurrentQueue<T>());
  19.                 }
  20.             }
  21.         }

  22.     }
复制代码



主要是想要这个线程安全同时实现先进先出、先进后出的集合:
  1. //先进先出(FIFO)
  2. BlockingCollection<int> bc = new BlockingCollection<int>(new ConcurrentQueue<int>());
  3. //先进后出(LIFO)
  4. BlockingCollection<int> bc2 = new BlockingCollection<int>(new ConcurrentStack<int>());
复制代码

评分

参与人数 1荣誉 +2 收起 理由
sfqxx + 2

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-10 23:05

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表