实现自定义集合类

实现自定义集合接口,需要继承ICollection和IEnumerable接口。

如果继承List则会丧失接口的使用。List并没有提供可供子类使用的protected成员。

在实现List接口的时候,new Add方法此时并没有覆盖掉List中的Add方法。

新建三个类

 

class Employee
    {
        public string Name { get; set; }
        public override string ToString()
        {
            return string.Format("{0}", this.Name);
        }
    }

 

class Employee01 : List<Employee>
    {
        public new void Add(Employee e)
        {
            e.Name += " Change";
            base.Add(e);
        }
    }
复制代码
class Employee02 : IEnumerable<Employee>, ICollection<Employee>
    {
        List<Employee> employeeList = new List<Employee>();
        public int Count => throw new NotImplementedException();

        public bool IsReadOnly => throw new NotImplementedException();
      //实现Add方法
        public void Add(Employee item)
        {
            item.Name += " Change";
            employeeList.Add(item);
        }
      //无用
        public void Clear()
        {
            throw new NotImplementedException();
        }
      //无用
        public bool Contains(Employee item)
        {
            throw new NotImplementedException();
        }
      //无用
        public void CopyTo(Employee[] array, int arrayIndex)
        {
            throw new NotImplementedException();
        }
       //迭代器
        public IEnumerator<Employee> GetEnumerator()
        {
            return employeeList.GetEnumerator();
        }
      //无用
        public bool Remove(Employee item)
        {
            throw new NotImplementedException();
        }
      //无用
        IEnumerator IEnumerable.GetEnumerator()
        {
            throw new NotImplementedException();
        }
    }
复制代码

这个Employee02实现ICollection和IEnumerable接口,实现的方法比较多,但大多都是与本次无关的

主函数代码:

复制代码
static void Main(string[] args)
        {
            #region
            Employee01 employee01 = new Employee01()
            {
                new Employee(){Name = "Machial"},
                new Employee(){Name = "Steven"}
            };// 此处在构造的时候就已经是往集合里面添加元素了
            foreach (var a in employee01)
            {
                Console.WriteLine(a.ToString());
            }
            Console.WriteLine();

            employee01.Add(new Employee { Name = "Lucy" });
            foreach (var a in employee01)
            {
                Console.WriteLine(a.ToString());
            }
            Console.WriteLine("Hello World!");
            Console.WriteLine();
            Console.WriteLine("书上的方法:");
            IList<Employee> employeeList = employee01;
            employeeList.Add(new Employee() { Name = "Lucy" });
            foreach (var a in employeeList)
            {
                Console.WriteLine(a.ToString());
            }
            Console.WriteLine();


            Console.WriteLine("List猜想:");
            IList<Employee> employee01Test = new Employee01() //这里我试过 IList<Employee> employee01Test = new List<Employee>()。结果当然是不成功的,new Add该new并不是覆盖父类List的Add方法,所以并没有起作用
            {
                new Employee(){Name = "Machial"},
                new Employee(){Name = "Steven"}
            };
            employee01Test.Add(new Employee() { Name = "Bens" });
            foreach (var a in employee01Test)
            {
                Console.WriteLine(a.ToString());
            }
            Console.WriteLine();
            #endregion

            #region
            Console.WriteLine();
            Console.WriteLine("Test测试02书上方法:");
            Employee02 employee02 = new Employee02()
            {
                new Employee(){Name = "Machial"},
                new Employee(){Name = "Steven"}
            };

            ICollection<Employee> employeeCollect = employee02;
            employeeCollect.Add(new Employee() { Name = "Jundiy" });
            foreach (var a in employeeCollect)
            {
                Console.WriteLine(a.ToString());
            }
            Console.WriteLine();

            employee02.Add(new Employee() { Name = "Dikan" });
            foreach (var a in employee02)
            {
                Console.WriteLine(a.ToString());
            }
            Console.WriteLine();

            Console.WriteLine("ICollection猜想:");
            ICollection<Employee> employee02Test = new Employee02()
            {
                new Employee(){Name = "Machial"},
                new Employee(){Name = "Steven"}
            };
            foreach (var a in employee02Test)
            {
                Console.WriteLine(a.ToString());
            }
            Console.WriteLine();
            #endregion
        }
复制代码

 

posted @   Wen_Chen  阅读(23)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示