C#方法重载-基于不同类型的参数的方法重载

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

namespace ConsoleApplication7
{
    class Test
    {
        static void Main(string[] args)
        {
            Test ts = new Test();
            Console.WriteLine("两个数最大的数是:{0}",ts.MaxNum(12, 33));
            Console.WriteLine("三个数最大的数是:{0}", ts.MaxNum(12, 35,9));
            Console.WriteLine("整数数组中最大的数是:{0}", ts.MaxArray(12, 45, 19,2));
            Console.WriteLine("浮点数组中最大的数是:{0}", ts.MaxArray(122.9, 45.0, 19.5, 2.0));  //传入不同类型数组
            Console.ReadKey();
        }

        int MaxNum(int x,int y)
        {
            if (x > y)
            {
                return x;
            }
            else
            {
                return y;
            }
        }
        int MaxNum(int x, int y, int z)
        {
            if (x > y && x > z)
            {
                return x;
            }
            if (y > x && y > z)
            {
                return y;
            }
            else
            {
                return z;
            }
        }

        int MaxArray(params int[] list)
        {
            int temp =list[0];
            foreach(int i in list)
            {
                if(temp<i)
                   temp=i;
            }
            return temp;
        }
        double MaxArray(params double[] list)
        {
            double temp = list[0];
            foreach (double d in list)
            {
                if (temp < d)
                    temp = d;
            }
            return temp;
        }
    }
}

posted on 2012-06-26 13:16  流星落  阅读(450)  评论(0编辑  收藏  举报

导航