欢迎访问我的博客 https://javascript.shop

重新学习基础:草稿2

原文发布时间为:2008-11-21 —— 来源于本人的百度文章 [由搬家工具导入]

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

namespace baidu
{
    class Class1
    {
        public static void Main()
        {
            string str1 = "abcdefghi";
            string str2 = string.Copy(str1);
            Console.WriteLine(str2);

            char[] des ={ 't', 's', 'e' };
            str1.CopyTo(1, des, 2, 1);
            Console.WriteLine(des);//tsb
            string[] a = str1.Split('c');
            foreach (string x in a)
                Console.WriteLine(x);

            StringBuilder sb1 = new StringBuilder("hi", 5);
            sb1.Capacity = 6;

            StringBuilder sb2 = new StringBuilder("hi");
            sb2.Append(" my dear");
            Console.WriteLine(sb2);

            sb2.AppendFormat("{0:c}", 12);
            Console.WriteLine(sb2);

            sb2.Insert(1, "ello");
            Console.WriteLine(sb2);

            sb2.Remove(2, 5);
            Console.WriteLine(sb2);

            sb2.Replace('¥', '$');
            Console.WriteLine(sb2);

            string[] week ={ "Sun", "Mon", "tue", "wends", "thirs", "fri", "sata" };
            DateTime now = DateTime.Now;
            Console.WriteLine("{0:现在是yyyy年MM月dd日,HH点mm分},{1}",now,week[(int)now.DayOfWeek]);

            DateTime past = new DateTime(2006, 1, 1, 22, 12, 12);
            TimeSpan ts = now - past;
            Console.WriteLine("{0},{1},{2}", ts, ts.Days, ts.TotalDays);

            Console.WriteLine(Math.Abs(-455214.44));
            Console.WriteLine(Math.Ceiling(2.32));
            Console.WriteLine(Math.Log(Math.E));
            Console.WriteLine(Math.Log10(10));
            Console.WriteLine(Math.Sqrt(4));
            Console.ReadLine();
        }
    }
}
------------------------------------------------------------------

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

namespace baidu
{
    public delegate void playGameHandler(object sender,EventArgs e);
    class Class2
    {
        public static void Main()
        {
            wang w = new wang();
            zhang z = new zhang();
            li l=new li();
            z.Playing += new playGameHandler(w.kouqian);
            z.Playing+=new playGameHandler(l.daren);
            z.playGame();
            Console.ReadLine();
        }
    }
    public class wang
    {
        public wang()
        {
           
        }
        public void kouqian(object sender, EventArgs e)
        {
            Console.WriteLine("kou qian ");
            zhang z = (zhang)sender;
            z.Money = z.Money - 50;
            Console.WriteLine(z.Money);
        }
    }
    public class zhang
    {
        public event playGameHandler Playing;
        private int money;
        public zhang()
        {
            money = 100;
        }
        public int Money
        {
            get { return money; }
            set { money = value; }
        }
        public void playGame()
        {
            Console.WriteLine("play game");
            EventArgs e = new EventArgs();
            onPlayGame(e);
        }
        protected virtual void onPlayGame(EventArgs e)
        {
            if (Playing != null)
                Playing(this, e);
        }
    }
    public class li
    {
        public void daren(object sender, EventArgs e)
        {
            zhang z = (zhang)sender;
            Console.WriteLine("da ren");
        }
    }
}
------------------------------------------------------------

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

namespace baidu
{
    public delegate void MieHuoEventHandler(object sender,HuoZhai e);
    class Class3
    {
        public static void Main()
        {
            Teacher t = new Teacher();
            t.MieHuo+=new MieHuoEventHandler(MieHuo);
            t.findHZ();
            Console.ReadLine();
        }
        public static void MieHuo(object sender, HuoZhai e)
        {
            Teacher t = (Teacher)sender;
            Console.WriteLine(t.name+ e.DiDian+e.JiRen+"MieHuo");
        }
    }

    public class HuoZhai : EventArgs
    {
        public string DiDian;
        public int JiRen;
        public HuoZhai(string dd, int jr)
        {
            this.DiDian = dd;
            this.JiRen = jr;
        }
    }
    public class Teacher
    {
        public event MieHuoEventHandler MieHuo;
        public string name = "Lily";
        public void findHZ()
        {
            HuoZhai hz = new HuoZhai("ShangHai", 4);
            BaoJin(hz);
        }
        public void BaoJin(HuoZhai hz)
        {
            if (MieHuo != null)
                MieHuo(this, hz);
        }
    }
}
------------------------------------------------------------------

使用委托和事件设计程序:小张和小王上课迟到,则小李就扣掉小张10分,扣掉小王50分,小王觉得自己被多扣了,再找回小李要求改正,小李同意了,补给小王40分,同时再多补5分予以补偿。

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

namespace baidu
{
    public delegate void LateEventHandler(object sender,EventArgs e);
    class Class4
    {       
        static void Main()
        {
            Zhang z = new Zhang();
            Wang w = new Wang();
            Li l = new Li();
            Console.WriteLine(z.name + "原来有" + z.Score + "分");
            Console.WriteLine(w.name + "原来有" + w.Score + "分");
            z.Zlate+=new LateEventHandler(l.zKouFen);
            w.Wlate += new LateEventHandler(l.wKouFen);
            w.Wask += new LateEventHandler(l.wZengFen);
            w.Wask += new LateEventHandler(l.wBuFen);
            z.Late();
            w.Late();
            w.Ask();
            Console.ReadLine();
        }
    }
   
    public class Zhang
    {
        public string name = "小张";
        private int _score=100;
        public event LateEventHandler Zlate;
        public int Score
        {
            get { return _score; }
            set { _score = value; }
        }

        public void Late()
        {
            Console.WriteLine(name + "迟到了");
            if (Zlate != null)
                Zlate(this, EventArgs.Empty);
        }
    }
    public class Wang
    {

        public string name = "小王";
        private int _score=100;
        public event LateEventHandler Wlate;
        public event LateEventHandler Wask;
        public int Score
        {
            get { return _score; }
            set { _score = value; }
        }

        public void Late()
        {
            Console.WriteLine(name + "迟到了");
            if (Wlate != null)
                Wlate(this,EventArgs.Empty);
        }
        public void Ask()
        {
            Console.WriteLine("小王觉得自己被多扣了,再找回小李要求改正");
            if (Wask != null)
                Wask(this, EventArgs.Empty);
        }
    }
    public class Li
    {
        public void zKouFen(object sender, EventArgs e)
        {
            Zhang z = (Zhang)sender;
            z.Score = z.Score - 10;
            Console.WriteLine("小李扣掉小张10分,现在小张还剩下" + z.Score + "分");
        }
        public void wKouFen(object sender, EventArgs e)
        {
            Wang w = (Wang)sender;
            w.Score = w.Score - 50;
            Console.WriteLine("小李扣掉小王50分,现在小王还剩下" + w.Score + "分");
        }
        public void wZengFen(object sender, EventArgs e)
        {
            Wang w = (Wang)sender;
            w.Score = w.Score + 40;
            Console.WriteLine("小李同意了,小李补给小王40分,现在小王还剩下" + w.Score + "分");
        }
        public void wBuFen(object sender, EventArgs e)
        {
            Wang w = (Wang)sender;
            w.Score = w.Score + 5;
            Console.WriteLine("同时再多补5分予以补偿,现在小王还剩下" + w.Score + "分");
        }
    }
}

posted @ 2017-07-11 00:33  孑孓子  阅读(99)  评论(0编辑  收藏  举报
欢迎访问我的博客 https://javascript.shop