笔记(显示实现接口,实现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
原创粉丝点击