C#委托的上机例子

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace detegateClass1
{
    class Student
    {
        private string name;

        private decimal score;

        public Student(string name, decimal score)
        {
            this.name = name;

            this.score = score;
        
        }

        /// <summary>
        /// 其中的一个比较方法,因为学生分数的比较,可能要比较多门学科,所以,这里这是列出其中的一们学科的比较方法。
        /// 委托就是要解决这种问题。
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static bool methodA(object a, object b) {

            Student sa = (Student)a;
            Student sb = (Student)b;

            return (sa.score > sb.score) ? true : false;
        
        }


        public override string ToString()
        {
            return string.Format(name+"  :  " + score);
        }
    }
}

 

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DelegateMethood = detegateClass1.Program.DelegateMethood;

namespace detegateClass1
{
    class ScoreSort
    {
        /// <summary>
        /// 这里采用冒泡比较法,由于比较的学科很多,所以要加入一个参数,要代表比较的是哪门的学科
        /// 由于这是一个"业务类",应该注重抽象的,不应涉及具体的东西,所以,这里可以用委托方法作为参数
        /// </summary>
        public static void sort(object[] array,DelegateMethood method) {

            for (int i = 0; i < array.Length; i++) {
                for (int j = i + 1; j < array.Length; j++) {
                    if (method(array[j], array[i])) {

                        object temp = array[i];
                        array[i] = array[j];
                        array[j] = temp;
                    
                    }
                }
             }
        
        }
    }
}

 

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace detegateClass1
{
    class Program
    {
        public delegate bool DelegateMethood(object a,object b);
        static void Main(string[] args)
        {
            Student[] students = { 
                                   new Student("A",100),
                                   new Student("B",90),
                                   new Student("C",80),
                                   new Student("D",70),
                                   new Student("E",60),
                                   new Student("F",50),
                                   new Student("G",40)
                                 
                                 };


            //在这里用定义的委托,进行方法的包装

            DelegateMethood dm = new DelegateMethood(Student.methodA);
            ScoreSort ss = new ScoreSort();

            for (int i = 0; i < students.Length; i++) {
                Console.WriteLine(students[i].ToString());
            }

            Console.ReadLine();


        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值