Esempio n. 1
0
        public void Show()
        {
            Student student = new Student();
            //student.SayHi("狗子", Student.Country.Chinese);
            //student.SayHi("宫本", Student.Country.Japanese);
            //student.SayHi("宋钟基", Student.Country.Korea);
            //student.SayHi("bbbb", Student.Country.Australian);

            //student.SayHiChinese("狗子");
            //student.SayHiJapanese("宫本");
            //student.SayHiKorea("宋钟基");
            //student.SayHiAustralian("bbbb");

            SayHiDelegate sayHiChinese = new SayHiDelegate(student.SayHiChinese);

            sayHiChinese("狗子");

            SayHiDelegate sayHiJapanese = new SayHiDelegate(student.SayHiJapanese);

            sayHiJapanese("宫本");

            SayHiDelegate sayHiKorea = new SayHiDelegate(student.SayHiKorea);

            sayHiKorea("宋钟基");

            SayHiDelegate sayHiAustralian = new SayHiDelegate(student.SayHiAustralian);

            sayHiAustralian("bbbb");
        }
Esempio n. 2
0
        //1 加参数判断---执行分支逻辑
        //2 加方法满足不同的场景   如果方法特别复杂,就推荐这个

        /// <summary>
        /// 既增加公共逻辑方便,又逻辑分离维护简单  鱼肉熊掌怎么兼得?
        /// 自上往下---逻辑解耦,方便维护升级
        ///
        /// 自下往上---代码重用,去掉重复代码
        /// </summary>
        public void SayHiPerfact(string name, SayHiDelegate method)
        {
            //Action<string,int>
            //Func<string>

            Console.WriteLine("prepare SayHi..");

            method.Invoke(name);
        }
Esempio n. 3
0
        static void Main(string[] args)
        {
            SayHiDelegate sayHi = null;

            sayHi  = new SayHiDelegate(SayHiEnglish);
            sayHi += new SayHiDelegate(SayHiSpanish);
            sayHi += new SayHiDelegate(SayHiFrench);
            sayHi();
        }
Esempio n. 4
0
        static void Main(string[] args)
        {
            SayHiDelegate sayHi = null;

            sayHi  = new SayHiDelegate(SayHiEnglish);
            sayHi += new SayHiDelegate(SayHiSpanish);
            sayHi += new SayHiDelegate(SayHiJapanese);
            sayHi += new SayHiDelegate(SayHiItalian);
            sayHi += new SayHiDelegate(SayHiGerman);
            sayHi += new SayHiDelegate(SayHiArabic);
            sayHi();
        }
Esempio n. 5
0
        static void Main(string[] args)
        {
            SayHiDelegate sayHi = null;

            sayHi  = new SayHiDelegate(SayHiDanish);
            sayHi += new SayHiDelegate(SayHiGerman);
            sayHi += new SayHiDelegate(SayHiEnglish);
            sayHi += new SayHiDelegate(SayHiSpanish);

            sayHi();

            Console.ReadKey();
        }
Esempio n. 6
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            DeleageDemoMyDelegate delegateDemo = new DeleageDemoMyDelegate();
            {
                SayHiDelegate method = new SayHiDelegate(delegateDemo.SayHiChinese);
                method += delegateDemo.SayHiEnglish;
                delegateDemo.SayHiShow("wwmin", method);
            }

            Action action1 = new Action(() => Console.WriteLine("www"));

            action1.Invoke();

            Func <string> func1 = new Func <string>(() => "wwmin");

            func1 += () => "liyue";
            Console.WriteLine(func1.Invoke());

            Console.ReadKey();
        }
 public void SayHiShow(string name, SayHiDelegate method)
 {
     Console.WriteLine("需要先举手");
     method.Invoke(name);
 }
Esempio n. 8
0
 /// <summary>
 /// 方法三、使用委托
 /// </summary>
 public void SayHiSenior(string name, SayHiDelegate sayHi)
 {
     Console.WriteLine("start loging");
     sayHi.Invoke(name);
     Console.WriteLine("end loging");
 }
Esempio n. 9
0
        // public void SayHi()
        // {
        //     SayHiPerfact(" 王大师", this.China);
        // }

        public void SayHiPerfact(string name, SayHiDelegate method)
        {
            Console.WriteLine("我是通用方法");
            method.Invoke(name);
        }
Esempio n. 10
0
        static void Main(string[] args)
        {
            PlayerInfoDel ronaldinho = new PlayerInfoDel(DisplayInformation);

            ronaldinho();

            PlayerInfoWithNameDel playerName = new PlayerInfoWithNameDel(DisplayInformation);

            playerName("Messi");

            PlayerInfoNameWithGoalsDel newPlayer = new PlayerInfoNameWithGoalsDel(DisplayInformation);

            newPlayer("Ronaldo", 60);
            newPlayer("Rooney", 25);

            PlayerBasedOnNumber number = new PlayerBasedOnNumber(DisplayInformation);

            Console.WriteLine(number(8));
            Console.WriteLine(number(10));

            PlayerInformationWithGoals            playerOne = new PlayerInformationWithGoals(DisplayInformation2);
            PlayerInformationBasedOnNumberAndClub playerTwo = new PlayerInformationBasedOnNumberAndClub(DisplayInformation2);

            //playerOne("Ronaldo", 50);
            //playerTwo(7, "Real Madrid","Portugal");

            Console.WriteLine(playerTwo.Method);

            foreach (var item in playerTwo.Method.GetParameters())
            {
                Console.WriteLine($"{item.ParameterType.Name}, {item.Name}, {item.Position}, {item.IsOptional}, {item.DefaultValue} ");
            }

            SayHiDelegate sayHi = null;

            sayHi  = new SayHiDelegate(SayHiEnglish);
            sayHi += new SayHiDelegate(SayHiSpanish);
            sayHi += new SayHiDelegate(SayHiJapanese);
            sayHi += new SayHiDelegate(SayHiItalian);
            sayHi += new SayHiDelegate(SayHiGerman);
            sayHi += new SayHiDelegate(SayHiArabic);

            sayHi();

            //genreic delegate
            Console.WriteLine("------Generic Delegate------");
            DisplayInfo <int> myNumber = new DisplayInfo <int>(DisplayValue);

            Console.WriteLine(myNumber(42));

            DisplayInfo <double> myDoubleNumber = new DisplayInfo <double>(DisplayValue);

            Console.WriteLine(myDoubleNumber(34.32));

            DisplayInfo <DateTime> myDate = new DisplayInfo <DateTime>(DisplayValue);

            Console.WriteLine(myDate(new DateTime(2010, 2, 23)));

            //Anonymous Method
            Multiply MultiplyNumber = delegate(int n) { { return(n * 3); } };

            Console.WriteLine(MultiplyNumber(10));

            DisplayMessage Message = delegate { Console.WriteLine("Hi from the anonymous method "); };

            Message();

            //Lambda\
            Console.WriteLine("-------Lambda-------");
            MultiplyLambda MultiplyNumberLambda = n => n * 3;

            Console.WriteLine(MultiplyNumber(10));

            DisplayMessageLambda MessageLambda = () => Console.WriteLine("Hi from anonymous lambda");

            Message();

            //Func Delegate
            Console.WriteLine("------Func Delegate-----");
            Func <int, int, int> funcOne = AddTwoNumbers;

            Console.WriteLine(AddTwoNumbers(3, 5));

            Func <int> funcTwo = AddTwoNumbers;

            Console.WriteLine(funcTwo());

            //Action Delegate
            Console.WriteLine("------Action Delegate--------");
            Action <int> actionOne = DisplayInformationActionDelegate;

            actionOne(16);

            Action actionTwo = DisplayInformationActionDelegate;

            actionTwo();

            //Predict Delegate
            Console.WriteLine("-------Predict Delegate------");
            Predicate <int> condition = IsAdmin;

            Console.WriteLine(condition(11));
            Console.WriteLine(condition(10));
        }