筆記(顯示實現接口,實現IComparable,IComparer接口來排序)

顯示實現接口
如果類繼承兩個接口中有相同方法,可用顯示接口實現

  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)
        {
            // return小於0則 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是按原本順序傳進來的。 return小於0則 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)
        {
            ......;
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章