JACK_C#_委託

        //創建委託
        public delegate void TextDelegate();
        public delegate void FuncDelegate(string str);
        public delegate bool Comparearr(T a,T b);

        //創建委託變量(字段)
        public TextDelegate textDelegate;
        public FuncDelegate funcDelegate;
        public Comparearr comparearr;

        public Text()
        {
        }

        public void Sort(T[] arr) {
            for (int i = 0i < arr.Length -1i++)
            {
                T min = arr[i];
                int minIndex = i;
                for (int j = i+1j < arr.Length - 1j++)
                {
                    if (comparearr(min,arr[j]))
                    {
                        min = arr[j];
                        minIndexj;
                    }
                }
                arr[minIndex] = arr[i];
                arr[i] = min;
            }

        }

        public class Student
        {
        public string name;
        public float score;
        public Student(string name,float score)
           {
            this.name = name;
            this.score = score;
           }
        }

class MainClass
    {
        public delegate void GreatDelegate(string name);
        public delegate void Printf<T>(T[] arr);
        public delegate bool StringEqual(string str1,string str2);


        public static void Main(string[] args)
        {
            int[] arr = { 3,67,2,78,23,54};

            Student stu_1 = new Student("JK"91);
            Student stu_2 = new Student("JaK"93);
            Student stu_3 = new Student("JcK"95);
            Student stu_4 = new Student("JacK",80);
            Student[] arr1 =new Student[]{ stu_1stu_2stu_3stu_4 };

            Printf<intprintf;
            printf = Arr;
            printf(arr);

            StringEqual equal;
            equal = Equal;
            bool a =equal("jack","JK");
            Console.WriteLine(a);

            //可以同時調用幾個方法
            GreatDelegate greet;
            greet = ChineseGreet;
            greet += EnglishGreet
            greet("JK");

            List<intlist = new List<int>(arr);
            list.Sort(Compare); //根據需求排序
            list.Reverse();
            foreach (int i in list)
            {
                Console.Write(" "i);
            }

            List<Studentlist1 = new List<Student>(arr1);
            list1.Sort(Compare);
            foreach (var t in list1)
            {
                Console.WriteLine(" "+t);
            }

            Text<Studenttext = new Text<Student>();
            text.comparearr = Compare1;
            text.Sort(arr1);
        }

        public static void ChineseGreet(string name)
        {
            Console.WriteLine("早上不好," + name);
        }

        public static void EnglishGreet(string name)
        {
            Console.WriteLine("all I need is fck  " + name);
        }

        public static void Arr<T>(T[] arr){
            foreach (T a in arr)
            {
                Console.Write(" "+a);
            }
        }


        public static bool Equal(string a,string b){
            if (a==b)
            {
                return true;
            }else {
                return false;
            }
        } 

        public static int Compare(int x,int y){
            if (x>y)
            {
                return -1;
            }else if (x<y)
            {
                return 1;
            }else {
                return 0;
            }
        }

        public static int Compare(Student xStudent y)
        {
            return x.name.CompareTo(y.name);
        }

        public static bool Compare1(Student xStudent y)
        {
            if(x.name.CompareTo(y.name)==1){
                return true;
            }
            return false;
        }
    }


       


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章