博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
方法重载
阅读量:4571 次
发布时间:2019-06-08

本文共 2463 字,大约阅读时间需要 8 分钟。

class Program    {        static void Main(string[] args)        {            //int[] arr = new int[3];            //TestParams(arr);            //int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };            ////  TestParams(arr);             //int[] a = new int[0];            //TestParams(1, 2, 3, 4, 5);             //int i = 12;            //TestOut(out i);            //Console.WriteLine(i);            //int[] arr = { 1, 2, 5, 4, 3 };            //int min = 0;            //int max = 0;            //GetMaxAndMin(arr, out max, out min);            //Console.WriteLine(min+":"+max);            int num=0;             TestRef(ref num);            Console.WriteLine(num);            //TestOut(out num);             //Console.WriteLine(num);                      Console.ReadKey();        }        //在同1个作用域下 不能定义相同名字的成员.        //什么情况下可以构成方法重载:        //1. 方法的名字一样 2.方法参数的个数或者类型或者顺序不一样 3.必须在同1个类中. 4.与返回值无关.        //可变参数 参数被params修饰 params只能用来修饰1维数组        //给可变参数赋值的时候 可以直接传递数组的元素.        //在调用的时候 会自动的将这些元素封装为1个数组 并将数组传递.        //可变参数必须放在参数列表的最后.         //ref 修饰方法的参数  在调用的时候必须在变量前面加上ref关键字. 只能传递变量不能传递常量.        //传递的时候 不是传递变量的值 而是传递变量的地址.        //out 也是传递的变量的地址.out必须在方法内为其赋值.ref可以修改其值也可以不修改.        //out侧重于输出 ref侧重于修改.         //out在传递之前可以不赋初始值 因为在方法中肯定会为out赋值.        //ref 在传递之前必须要有值 因为在方法中有可能会用到这个参数的值.        static void TestOut(out int i)        {            string str = "s";            if (str == "s")            {                i = 1;            }            else            {                i = 2;            }         }         static void TestRef(ref int i)        {            i = i + 1;                   }         //冒泡排序        static void GetMaxAndMin(int[] arr, out int max, out int min)        {             for (int i = 0; i < arr.Length - 1; i++)            {                for (int j = 0; j < arr.Length - 1 - i; j++)                {                    if (arr[j] > arr[j + 1])                    {                        int temp = arr[j];                        arr[j] = arr[j + 1];                        arr[j + 1] = temp;                    }                }            }            max = arr[arr.Length - 1];            min = arr[0];        }        //static void TestParams(int i, int j, params int[] arr)        //{        //}         static void Test(int i, string str)        {        }        static void Test()        {            Console.WriteLine("B");        }    }

 

转载于:https://www.cnblogs.com/kongsq/p/5866681.html

你可能感兴趣的文章
creat-react-app搭建的项目中按需引入antd以及配置Less和如何修改antd的主题色
查看>>
IIS安装
查看>>
html块级元素和行级元素的区别和使用
查看>>
for循环嵌套
查看>>
寒冬夜行人
查看>>
bat for循环
查看>>
poj1151 Atlantis
查看>>
HTML页面之间的参数传递
查看>>
java面试题集锦
查看>>
scikit-learn:4.2.3. Text feature extraction
查看>>
Spring Security构建Rest服务-0800-Spring Security图片验证码
查看>>
AE待整理
查看>>
java8中规范的四大函数式接口
查看>>
分类---Logistic Regression
查看>>
【bzoj1270】[BeijingWc2008]雷涛的小猫 dp
查看>>
35.Docker安装Mysql挂载Host Volume
查看>>
ring buffer 亲测好用 C++ 11
查看>>
大型网站架构系列:电商网站架构案例(3)
查看>>
java 延时的几种方法方法
查看>>
数据库工具
查看>>