zoukankan      html  css  js  c++  java
  • delegate

    // 可以接受若干参数的委托
        public delegate double Method( params int[] numbers);

        public class Demo
        {


            public double Times( params int[] numbers)
            {
                double result = 1;
                foreach (int i in numbers)
                    result *= i;
                return result;
            }

            public double Div(params int[] numbers)
            {
                if (numbers == null || numbers.Length == 0)
                    throw new InvalidOperationException("参数错误");
               
                if (numbers.Length == 1)
                    return numbers[0];

                double first = numbers[0];
                int length = numbers.Length;

                for (int i = 1; i < length; i++)
                {
                    int x = numbers[i];
                    if (x == 0)
                        throw new InvalidOperationException("错误的参数");

                    first /= x;
                }

                return first;
            }
        }


        public class Calculator
        {

            public Method Times;
            public Method Div;

            public Calculator()
            {
            }

            public double MyTimes(params int[] numbers)
            {
                return this.Times(numbers);
            }

            public double MyDiv(params int[] numbers)
            {
                return this.Div(numbers);
            }

        }

        class Program
        {


            static void Main(string[] args)
            {
                // 装配
                Calculator calculator = new Calculator();

                Demo demo = new Demo();

                calculator.Times = demo.Times;
                calculator.Div = demo.Div;

                // 使用计算器
                double r = calculator.MyTimes(1, 2, 3, 4);
                Console.WriteLine(r);

                double r2 = calculator.MyDiv(12, 2, 3);
                Console.WriteLine(r2);
            }}

  • 相关阅读:
    smarty中ifelse、foreach以及获取数组中键值名的一个实例
    smarty逻辑运算符
    python strip()函数 介绍
    (转)论python工厂函数与内建函数
    数据结构哈希表(转)
    哈希表算法的编写
    哈希表(转)
    平衡二叉树的旋转操作
    并查集详解(转)
    Java数组技巧攻略
  • 原文地址:https://www.cnblogs.com/lovehappy/p/2168661.html
Copyright © 2011-2022 走看看