实现自定义集合类
实现自定义集合接口,需要继承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 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】