zoukankan      html  css  js  c++  java
  • 一天一小步_我学C#入门精典_第十天

    1.函数的重载

    函数的重载指需要让多个函数操作不同类型的变量.函数重载允许创建同名的多个函数.这些函数可使用不同的参数类型.

    示例:

    class Program
    {
        
    static void Main(string[] args)
        {
            
    int[] myArray = {1836259302};
            
    int maxVal = MaxValue(myArray);
            Console.WriteLine(
    "The maximum value in myArray is {0}", maxVal);
            Console.ReadKey();
        }
        
    /// <summary>
        
    /// 求一个int型数组中的最大值
        
    /// </summary>
        
    /// <param name="intArray">int型数组</param>
        
    /// <returns>返回数组中的最大值</returns>
        static int MaxValue(int[] intArray)
        {
            
    int maxVal = intArray[0];
            
    for (int i = 1; i < intArray.Length; i++)
            {
                
    if (intArray[i] > maaxVal)
                {
                    maxVal 
    = intArray[i];
                }
                
    return maxVal;
            }
        }
        
    /// <summary>
        
    /// 求一个double数组中的最大值
        
    /// </summary>
        
    /// <param name="doubleArray">double型数组</param>
        
    /// <returns>返回数组中的最大值</returns>
        static double MaxValue(double[] doubleArray)
        {
            
    double maxVal = doubleArray[0];
            
    for (int i = 1; i < doubleArray.Length; i++)
            {
                
    if(doubleArray[i] >maxVal)
                {
                    maxVal 
    = doubleArray[i];
                }
                returrn maxVal;
            }
        }
    }

     *根据传进的参数类型来确定调用的函数.如果参数为int型数组,则调用static int MaxValue(int[] intArray),如果参数为double型则调用static double MaxValue(double[] doubleArray)

     2.委托

    委托是一种可以把引用存储为函数的类型. 委托的声明非常类似于函数,但不带函数体,且要使用delegate关键字.委托的声明指定了一个函数签名,其中包含一个返回类型和参数列表.

    示例:

    #region test17 委托的使用

    public void test17()
    {
        
    //声明委托的一个变量
        ProcessDelegate process;
        Console.WriteLine(
    "Enter 2 numbers separated with a comma(逗号):");
        
    string input = Console.ReadLine();
        
    int commaPos = input.IndexOf(',');//用逗号将输入的两个数分开
        double param1 = Convert.ToDouble(input.Substring(0, commaPos));
        
    double param2 = Convert.ToDouble(input.Substring(commaPos + 1, input.Length - commaPos - 1));
        relocal:
        Console.WriteLine(
    "Enter M to multiply or D to divide: ");//输入M则进行乘法运算,输入D则进行除法运算.
        input = Console.ReadLine();
        
    if (input.ToUpper() == "M")
        {
            process 
    = new ProcessDelegate(Multiply);
        }
        
    else if (input.ToUpper() == "D")
        {
            process 
    = new ProcessDelegate(Divide);
        }
        
    else
        {
            
    //如果输入的不是M或D则重新输入,goto语句尽量还是少用吧.
            goto relocal;
        }
        Console.WriteLine(
    "Result : {0}", process(param1, param2));
        Console.ReadKey();
    }
    /// <summary>
    /// 委托名:ProcessDelegate
    /// 其签名与Multiply和Divide两个函数的签名相匹配
    /// </summary>
    /// <param name="param1">参数1</param>
    /// <param name="param2">参数2</param>
    /// <returns></returns>
    delegate double ProcessDelegate(double param1, double param2);
    /// <summary>
    /// 方法名:Multiply
    /// 功    能:实现乘法运算
    /// </summary>
    /// <param name="param1">乘数1</param>
    /// <param name="param2">乘数2</param>
    /// <returns>返回乘积</returns>
    static double Multiply(double param1, double param2)
    {
        
    return param1 * param2;
    }
    /// <summary>
    /// 方法名:Divide
    /// 功    能:实现除法运算
    /// </summary>
    /// <param name="param1">被除数</param>
    /// <param name="param2">除数</param>
    /// <returns>返回运算的商</returns>
    static double Divide(double param1, double param2)
    {
        
    return param1 / param2;
    }
    #endregion
  • 相关阅读:
    jquery冲突细节
    最懂中文的H5前端框架amazeUI
    IT Girl
    json_encode注意
    YII2 Activedataprovider 类分页的使用
    Yii框架,在页面输出执行sql语句,方便调试
    yii2的GridView和ActiveDataProvider具体使用
    文件压缩工具类
    将dubbo中使用的动态代理作为工具类
    spring中使用动态代理(AOP)
  • 原文地址:https://www.cnblogs.com/soso_ak/p/1443493.html
Copyright © 2011-2022 走看看