笔记(显示实现接口,实现IComparable,IComparer接口来排序)
来源:互联网 发布:测试反应力软件 编辑:程序博客网 时间:2024/05/01 22:12
显示实现接口
如果类继承两个接口中有相同方法,可用显示接口实现
class Program { static void Main(string[] args) { A a = new A(); a.run(); // "run" B b = new A(); b.run(); // "run B" C c = new A(); c.run();// "run C" Console.ReadKey(); } } public interface B { void run(); } public interface C { void run(); } public class A : B, C { public void run() { Console.WriteLine("run "); } void B.run()//不能有修饰符,是私有的 { Console.WriteLine("run B"); } void C.run() { Console.WriteLine("run C"); } }
接口显示实现的方法只能由接口直接调用 ,如果将public void run()注释, A a = new A();a.run()这里的a不能.出run。都是因为 void B.run() 和 void C.run()在类A里是私有的。
实现IComparable,IComparer接口来排序
class Program { static void Main(string[] args) { ArrayList array =new ArrayList(); Person a = new Person(); a.name = "aaa"; a.age = 18; Person b = new Person(); b.name = "b"; b.age = 26; Person c = new Person(); c.name = "cc"; c.age = 20; array.Add(a); array.Add(b); array.Add(c); //array.Sort实现排序 array里的元素必须实现IComparable接口,int,string能排序是因为本身已实现了 array.Sort(); // 排序完为 a c b //实现IComparable接口只能按照一种方式排序,可以写一些类实现IComparer接口(比较器),每一个类表示一种排序 array.Sort(new PersonSortByName()); // 排序完为 a b c array.Sort(new PersonSortByNameLength());// 排序完为 b c a array.Sort(new PersonSortByAge());//排序完为 b c a } } public class Person:IComparable { public string name; public int age; public int CompareTo(object obj) { // return0则 this排前面,大于0则this排后面 Person p = obj as Person; if (p != null) return this.age - p.age;//按age升序。若改为 p.age-this.age则是降序 return 0; } } public class PersonSortByName : IComparer { public int Compare(object x, object y) { //x,y是按原本顺序传进来的。 return0则 x排前面,大于0则x排后面 Person p1 = x as Person; Person p2 = y as Person; if (p1 != null && p2 != null) return p1.name.CompareTo(p2.name);//按字符升序 return 0; } } //下面类就是比较器 public class PersonSortByNameLength : IComparer { public int Compare(object x, object y) { Person p1 = x as Person; Person p2 = y as Person; if (p1 != null && p2 != null) return p1.name.Length - p2.name.Length;//按name字符长度升序 return 0; } } public class PersonSortByAge: IComparer { public int Compare(object x, object y) { Person p1 = x as Person; Person p2 = y as Person; if (p1 != null && p2 != null) return p2.age- p1.age;//按age降序 return 0; } }
//如果是List<T>泛型 List<Person> list = new List<Person>(); list.Add(a); list.Add(b); list.Add(c); //用泛型比较器泛型的比较器 list.Sort(new PersonSortByNameT()); //泛型也可以用Lamdba表达式来实现 list.Sort((m, n) => n.name.Length-m.name.Length);//实现IComparable<T>和IComparer<T>接口如下 public class Person:IComparable<Person> { public string name; public int age; public int CompareTo(Person other) { ......; } } public class PersonSortByName : IComparer<Person> { public int Compare(Person x, Person y) { ......; } }
阅读全文
0 0
- 笔记(显示实现接口,实现IComparable,IComparer接口来排序)
- Icomparable与Icomparer实现显示接口以显示接口排序为准
- Effective C# 使用IComparable和IComparer接口实现排序关系
- C#学习笔记 IComparable接口 IComparable<T>接口 IComparer接口
- 《Effective C#》 Item 26:对需要排序的对象实现IComparable和IComparer接口
- IComparable 和IComparer 接口实现数组的自定义排列
- c# 实现IComparable、IComparer接口、Comparer类的详解
- IComparable和Icomparer接口
- IComparable和IComparer接口
- IComparable 接口的实现
- 结合IComparer接口实现对ListBox排序
- C# List<> 实现 IComparer 接口 排序
- 使用IComparable和IComparer接口对集合进行排序
- 对象的比较与排序:IComparable和IComparer接口
- 对象的比较与排序:IComparable和IComparer接口
- IComparable接口和IComparer接口(一)
- C#笔记25:比较和排序(IComparable和IComparer以及它们的泛型实现)
- 实现Icomparable接口(C#)
- HDU 4641 sam…………好恶心
- linux的删除的小技巧
- 大数据分析的未来图景:万物皆可分析
- Traceback (most recent call last): File "/usr/bin/firewall-cmd", line 24, in <module> from gi.
- WebApi接口返回json,xml,text纯文本等
- 笔记(显示实现接口,实现IComparable,IComparer接口来排序)
- CentOS7 LAMP环境搭建
- spring IOC三种机制
- state和props的区别__react
- SHELL脚本编程进阶(一)
- 使用原生的java类进行URL解析
- 指针与引用的区别
- erlang的re模块
- AngularJS