Ejemplo n.º 1
0
        public static void Main1()
        {
            //实例化委托
            OrderDelegate orderDelegate = new OrderDelegate(Order.BuyFood);

            //向委托中注册方法
            orderDelegate += Order.BuyCake;
            orderDelegate += Order.BuyFlower;

            //还可以撤销.
            //orderDelegate -= Order.BuyFlower;

            //调用委托
            orderDelegate();


            //匿名委托
            double       length = double.Parse(Console.ReadLine());
            double       width  = double.Parse(Console.ReadLine());
            AreaDelegate ad     = delegate {
                Console.WriteLine("长方形的面积为:" + length * width);
            };

            ad(length, width);
        }
Ejemplo n.º 2
0
        private void btnArea_Click(object sender, EventArgs e)
        {
            AreaDelegate WoodArea = r => (r + 1) * (r + 1) * Math.PI;
            AreaDelegate GoldArea = r => (r + 0.05) * (r + 0.05) * Math.PI;

            Console.WriteLine(Area(5, WoodArea));
            Console.WriteLine(Area(5, GoldArea));
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            AreaDelegate ad1 = new AreaDelegate(RectangleArea);

            ShapeArea.PrintShapeArea("사각형", 20, 30, ad1);

            AreaDelegate ad2 = new AreaDelegate(TriangleArea);

            ShapeArea.PrintShapeArea("삼각형", 20, 30, ad2);
        }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            Console.WriteLine("请输入长方形的长:");
            double length = double.Parse(Console.ReadLine());

            Console.WriteLine("请输入长方形的宽:");
            double       width        = double.Parse(Console.ReadLine());
            AreaDelegate areaDelegate = delegate  //匿名委托
            {
                Console.WriteLine("长方形的面积为:" + length * width);
            };

            areaDelegate(length, width);


            MyDelegate myDelegate = new MyDelegate(SayHello);  //命名方法委托
        }
Ejemplo n.º 5
0
        private void btnCircleArea_Click(object sender, EventArgs e)//lambda運算式
        {
            AreaDelegate CircleArea = r => r * r * Math.PI;

            MessageBox.Show($"圓面積:{CircleArea(5)}");
            MessageBox.Show($"圓面積:{AreaCircle(10)}");

            MessageBox.Show($"圓面積:{CustomCircleArea(5, r => r * r * Math.PI)}");
            MessageBox.Show($"圓面積:{CustomCircleArea(5, r => (r-0.1) * (r-0.1) * Math.PI)}");


            //AreaDelegate SqyareArea = s => s * s;
            //MessageBox.Show($"正方形面積:{SqyareArea(5)}");


            //AreaDelegate2 RectangleArea = (w, l) => w * l;
            //MessageBox.Show($"正方形面積:{RectangleArea(5,10)}");
        }
Ejemplo n.º 6
0
        static void Main(string[] args)
        {
            //Step-2: Instantiating the delegate
            AreaDelegate area = new AreaDelegate(Rectangle.CalculateArea);
            // OR
            //Step-3: Binding method to delegate instance
            AreaDelegate area1 = Rectangle.CalculateArea;

            // Step-3: Invoking the Delegate by using invoke method
            Console.WriteLine("Using invoke method");
            Console.WriteLine("Area of Rectangle is {0}", area.Invoke(10.10, 20.10));
            // OR
            Console.WriteLine("-------------------------------------------------");
            // Step-3: Invoking the Delegate without using the invoke method
            Console.WriteLine("Without using invoke method");
            Console.WriteLine("Area of Rectangle is {0} ", area1(10.10, 20.10));
            Console.ReadLine();
        }
Ejemplo n.º 7
0
 public void PrintShapeArea(string name, double a, double b, AreaDelegate areaDelegate)
 {
     Console.WriteLine($"{name} 도형의 넓이 : {areaDelegate(a, b)}");
 }
Ejemplo n.º 8
0
 double CustomCircleArea(double r, AreaDelegate ad)//呼叫方法
 {
     return(ad(r));
 }
Ejemplo n.º 9
0
 public void PrintShapeArea <T>(string name, T a, T b, AreaDelegate <T> areaDelegate)
 {
     Console.WriteLine($"{name} 도형의 넓이 : {areaDelegate(a, b)}");
 }
Ejemplo n.º 10
0
    static void Main(string[] args)
    {
#if false
        xj obj = new xj();
        obj.Age    = 36;
        obj.Height = 158;
        obj.Nation = "Japan";
        Console.WriteLine("Age={0},Height={1}", obj.Age, obj.Height);
#endif

#if false
        int[,] m_array = new int[2, 3];

        for (int i = 0; i < m_array.Length; i++)
        {
            m_array[i / 3, i % 3] = i;
        }

        for (int i = 0; i < m_array.Length; i++)
        {
            Console.WriteLine("{0}, ", m_array[i / 3, i % 3]);
        }
#endif

#if false
        int[] num = { 2, 4, 8 };

        foreach (int x in num)
        {
            Console.WriteLine(x);
        }
#endif

#if false
        Shapes shp = new Shapes();
        shp.Add(5);
        shp.Add(4);
        shp.Add(3);
        shp.Add(2);
        shp.Add(1);
        Console.WriteLine(shp[2]);
#endif

#if false
// 装箱和拆箱实验
//在 C# 语言中数据类型分为值类型和引用类型,将值类型转换为引用类型的操作称为装箱,相应地将引用类型转换成值类型称为拆箱。

        int    a   = 101;
        string str = a.ToString();     //装箱
        float  b   = float.Parse(str); //拆箱

        Console.WriteLine("a={0}, b={1}", a, b);

        int    val = 100;
        object obj = val;//装箱
        Console.WriteLine("对象的值 = {0}", obj);

        int num = (int)obj;//拆箱
        Console.WriteLine("num: {0}", num);
#endif

#if false
// 第1种委托:命名方法委托

        Mydel del_a = new Mydel(Shapes.showXj);       // 委托实例化, 代理类的静态方法
        del_a();                                      // 调用委托实例

        Mydel del_b = new Mydel(new Shapes().showJs); // 委托实例化,代理一个对象的方法
        del_b();
#endif

#if false
        BookDelegate bookDelegate = new BookDelegate(Book.BookSort);
        Book[]       books        = new Book[3];
        books[0] = new Book("计算机应用", 50);
        books[1] = new Book("C# 教程", 59);
        books[2] = new Book("VS2015应用", 49);
        bookDelegate(books);
        foreach (Book bk in books)
        {
            Console.WriteLine(bk);
        }
#endif

#if false
// 第2种委托:多播委托。
// 即调用委托实例时,可同时调用所代理的多个方法。可以向委托实例添加、删除方法
        DelegateLearn delObj = new DelegateLearn();
        while (true)
        {
            Console.WriteLine("A - add, R - remove, Q - quit:");
            switch (Console.ReadKey().Key)
            {
            case ConsoleKey.A:
                delObj.AddFlower();
                break;

            case ConsoleKey.R:
                delObj.RemoveFlower();
                break;

            case ConsoleKey.Q:
                return;

            default:
                delObj.showOrder();
                break;
            }
        }
#endif

#if true
// 第3种委托:匿名委托。就是实例化委托时,直接给出代码块,而不必定义方法
// 匿名委托不是为了代码重用,一般用于实现一些仅需要使用一次委托中代码的情况,并且代码比较少
        AreaDelegate areaDlg = delegate(double length, double width){
            Console.WriteLine("Rectangle Area: " + length * width);
        };
        areaDlg(3, 6);
#endif
    }
Ejemplo n.º 11
0
 double Area(double r, AreaDelegate ad)
 {
     return(ad(r));
 }
Ejemplo n.º 12
0
 private void circleButton_Click(object sender, RoutedEventArgs e)
 {
     areaMethod = CircleArea;
     input      = CrRadius;
 }
Ejemplo n.º 13
0
 private void triangleButton_Click(object sender, RoutedEventArgs e)
 {
     areaMethod = TriangleArea;
     input      = TriBase;
 }
Ejemplo n.º 14
0
 private void squareButton_Click(object sender, RoutedEventArgs e)
 {
     areaMethod = SquareArea;
     input      = SqBase;
 }
Ejemplo n.º 15
0
        private void btnCircle_Click(object sender, EventArgs e)
        {
            AreaDelegate circleArea = r => r * r * 3.14;

            MessageBox.Show(string.Format("圓形面積為:{0}", circleArea(5)));
        }