应用场景:
用户有一张信用卡,信用卡有一个总额度;
 每个月会有信用卡账单显示月消费总额(月消费总额是小于信用卡总额度的);
 用户有若干储蓄卡,可选择某张储蓄卡进行还款;
 还款是指从储蓄卡中划走信用卡的月消费总额到信用卡;
 如果储蓄卡余额不足则
 要求:①必须使用委托②事件的触发方式是每个月的到期还款日;还款动作不成功。
代码:
信用卡类:
    class Xinyong
    {
        private float sum;
        private float xiaofei;
        public Xinyong(float a,float b)
        {
            sum = a;
            xiaofei = b;
        }
        public void Zhangdan()
        {
            Console.WriteLine("--本月消费账单--");
            Console.WriteLine("本月消费:");
            Console.WriteLine(xiaofei);
            Console.WriteLine("----------------");
        }
    }
 
储蓄卡类:
class Chuxu
    {
        private float sum;
        private float xiaofei;
        public Chuxu(float a,float b)
        {
            sum = a;
            xiaofei=b;
        }
        public void Huankuan()
        {
            if(sum<xiaofei )
            {
                Console.WriteLine("还款失败,请使用其他储蓄卡");
            }
            else
            {
                sum -= xiaofei;
                Console.WriteLine("还款成功!");
                Console.WriteLine("储蓄卡余额:");
                Console.WriteLine(sum);
            }
        }
    }
 
委托类:
class Delegate
    {
        // 定义委托
        public delegate void MeDelegate();
        // 定义事件
        public event MeDelegate myevent;
        public void Notify()
        {
            // 如果事件不为 null
            if (myevent!= null)
            {
                Console.WriteLine("#已到本月还款日期#");
                // 触发事件
                myevent();
            }
        }
    }
 
主函数:
    class Test
    {
        static void Main(string[] args)
        {
            float xiaofei;
            string a,b;
            // 委托的对象
            Delegate obj1 = new Delegate();
            Console.WriteLine("请输入消费:");
            a=Console.ReadLine();
            xiaofei=float.Parse(a);
            Xinyong obj2 = new Xinyong(1000,xiaofei);
            Chuxu obj3 = new Chuxu(1000, xiaofei);
            Chuxu obj4 = new Chuxu(500, xiaofei);
            obj1.myevent += new Delegate.MeDelegate(obj2.Zhangdan);
            Console.WriteLine("请选择储蓄卡1还是储蓄卡2:");
            b = Console.ReadLine();
            if(b=="1")
            {
                obj1.myevent += new Delegate.MeDelegate(obj3.Huankuan);
            }
            else if(b=="2")
            {
                obj1.myevent += new Delegate.MeDelegate(obj4.Huankuan);
            }
            else
            {
                Console.WriteLine("储蓄卡输入错误!");
            }
            obj1.Notify();
        }
    }
 
运行截图:

 
源码的clone地址:
https://github.com/pan20174/pan20174/tree/main










