泛型委托学习进程

首先先回顾委托的使用过程步骤:

委托使用总结:

(1)     委托声明(定义一个函数原型:返回值+参数类型和个数)注:在类的外部——中介(房产中介商)

(2)     根据委托定义“具体”的方法——————房源   注:在类中定义方法

(3)     创建委托对象,关联“具体方法”———中介商拥有房源  注意:在主函数中操作 

                                  第一种方式:使用new初始化。第二种方式:直接给委托变量赋值方法  

(4)     通过委托去调用方法(而不是直接调用方法)——————中介带人看房

开始学习:通过一个需求来建立使用泛型委托的思想  需求为:求任意数组的最大值

  首先,是我们过去使用的普通方法:需要啥方法就写啥方法,然后调用。  这里我们就不多说,大家应该清楚。。。。。

 其次,使用委托

 public delegate int toMaxod(object o1, object o2);
    class Program
    {
        static void Main(string[] args)
        {
            //需求为求任意数组的最大值 通过委托来实现
            object[] nu = { 0, 1, 2, 2, 2, 3, 4 };
            object re = GetMax(nu, toMaxok);
            Console.WriteLine("Max={0}", re);
            Console.ReadLine();
        }
        public static object GetMax(object[] nums, toMaxod del)
        {
            object max = nums[0];
            for (int i = 0; i < nums.Length; i++)
            {
                if (del(max, nums[i]) < 0)
                {
                    max = nums[i];
                }
            }
            return max;
        }

        private static int toMaxok(object o1, object o2)
        {
            int n1 = (int)o1;
            int n2 = (int)o2;
            return n1 - n2;
        }

    }

但这只是实现了整型数组的比较

字符串数组的比较为(注释部分为整型数组的):

public delegate int toMaxod(object o1, object o2);    //object 为所有对象
    class Program
    {
        static void Main(string[] args)
        {
            //需求为求任意数组的最大值 通过委托来实现

            //object[] nus = { 0, 1, 2, 2, 2, 3, 4 };  //整型数组
            //object re = GetMax(nus, toMaxok); //调用比较整型数组的方法
            //Console.WriteLine("Max={0}", re);

object[] nus = {"abc", "fdsfdsds","fdsfdsfdsfdsfdsfds","fdsfds"};//字符串数组 object res = GetMax(nus, toMaxok2);
/////////////////////////通过匿名函数实现
//object res = GetMax(o, delegate(object o1, object o2) {
            //    string s1 = (string)o1;
            //    string s2 = (string)o2;
            //    return s1.Length - s2.Length;
            //});
////////////////////////通过lambda表达式来实现
// object res = GetMax(o, (object o1,object o2) => {
               // string s1 = (string)o1;
               // string s2 = (string)o2;
               // return s1.Length - s2.Length;
            //});
Console.WriteLine(
"StrMax={0}", res); Console.ReadLine(); Console.ReadLine(); } public static object GetMax(object[] nums, toMaxod del) { object max = nums[0]; for (int i = 0; i < nums.Length; i++) { if (del(max, nums[i]) < 0) { max = nums[i]; } } return max; } private static int toMaxok2(object o1, object o2)//字符串数组类型比较 { string s1 = (string)o1; string s2 = (string)o2; return s1.Length - s2.Length; } //private static int toMaxok(object o1, object o2)//实现整型数组的比较 //{ // int n1 = (int)o1; // int n2 = (int)o2; // return n1 - n2; //} }

在上面的例子中我们使用object(表示所有对象)在填写参数无法明确需要填写的类型因为都是object类型,难以区分我此时需要填写的具体类型。

那么,我们考虑有什么办法可以实现在填写时自动提示我需要填写什么类型的对象呢????

使用泛型T 即使用泛型委托

使用泛型委托的代码为:

public delegate int DelCompare<T>(T t1, T t2);
    // public delegate int DelCompare(object o1, object o2);
    class Program
    {
        static void Main(string[] args)
        {
            //int[] nums = { 1, 2, 3, 4, 5 };
            //int max = GetMax<int>(nums, Compare1);
            //Console.WriteLine(max);

            string[] names = { "abcdefg", "fdsfds", "fdsfdsfdsfdsfdsfdsfdsfsd" };
            string max1 = GetMax<string>(names, (string s1, string s2) =>
            {
                return s1.Length - s2.Length;
            });
            Console.WriteLine(max1);
            Console.ReadKey();
        }
        public static T GetMax<T>(T[] nums, DelCompare<T> del)
        {
            T max = nums[0];
            for (int i = 0; i < nums.Length; i++)
            {
                //要传一个比较的方法
                if (del(max, nums[i]) < 0)
                {
                    max = nums[i];
                }
            }
            return max;
        }


        //public static int Compare1(int n1, int n2)//具体的int类型比较而不是object类型
        //{
        //    return n1 - n2;
        //}
    }

可能还会想不透Object已经是所有类型为什么还用泛型<T>表示呀???

举个简单的例子:List<T>的例子

比如你声明一个List<T>
然后当你试着往List添加Student对象
那么这个<T>就相当于变成了<Student>,你只能向其中插入Student对象
你向这个List添加别的对象虽然也默认继承Object但是不是Student类型所以不能插入
但如果你一开始就声明了List<Object>
那么你插入任何对象都是没有问题了,那么也就失去了泛型→规范类型的意义了。

 

posted @ 2017-02-06 18:46  WFaceBoss  阅读(254)  评论(0编辑  收藏  举报