一、咱们在Main()函数中,调用Test()函数,咱们管Main()函数称之为调用者,
管Test()函数称之为被调用者。
若是被调用者想要获得调用者的值:
1)、传递参数。
2)、使用静态字段来模拟全局变量。(多个方法都须要使用同一个变量)
语法:在类下面
public static int _number = 10;
若是调用者想要获得被调用者的值:
1)、返回值数组
class Program { ////使用静态字段模拟全局变量 //public static int _number = 3; static void Main(string[] args) { //int a = 3; //int sum = Sum(a); //Console.WriteLine(sum); //Console.ReadKey(); //计算是不是闰年 Console.WriteLine("请输入一个年份,计算是不是闰年"); int year = Convert.ToInt32(Console.ReadLine()); bool b = IsRun(year); Console.WriteLine(b); Console.ReadKey(); } /// <summary> /// 接收用户输入,计算是不是闰年 /// </summary> /// <param name="year">用户输入的年份</param> /// <returns>是或否</returns> public static bool IsRun(int year) { bool b = (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0); return b; } ///// <summary> ///// 计算a+5 ///// </summary> ///// <param name="a">传进来的变量a</param> ///// <returns>返回计算后的结果</returns> //public static int Sum(int a) //{ // return a = a + 5; //} }
二、无论是实参仍是形参,都是在内存中开辟了空间的。ide
三、方法的功能必定要单一。
GetMax(int n1,int n2)
方法中最忌讳的就是出现提示用户输入的字眼,除非避免不了的函数
方法的四个练习code
class Program { static void Main(string[] args) { //1.读取输入的整数,定义成方法,屡次调用(若是用户输入的数字,则返回,不然提示用户从新输入) Console.WriteLine("请输入一个数字"); string input = Console.ReadLine(); int number = GetNumber(input); Console.WriteLine(number); Console.ReadKey(); //2.还记得学循环时作的那道题吗?只容许用户输入y/n,请改为方法 Console.WriteLine("请输入y/n"); string str = Console.ReadLine(); string strTwo = ShuRu(str); Console.WriteLine(strTwo); Console.ReadKey(); //3.查找两个整数中的最大值:intMax(int i1,int i2) Console.WriteLine("对比两个数的最大值"); int i1 = 110; int i2 = 20; int max = GetMax(i1, i2); Console.WriteLine(max); Console.ReadKey(); //4.计算输入数组的和:int Sum(int[] values) int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }; int sum = GetSum(nums); Console.WriteLine(sum); Console.ReadKey(); } #region 第一题的方法 /// <summary> /// 将用户输入转换成数字并返回 /// 不能转换要求从新输。 /// </summary> /// <param name="i">用户输入</param> /// <returns></returns> public static int GetNumber(string i) { while (true) { try { int number = Convert.ToInt32(i); return number; } catch { Console.WriteLine("输入的内容不能转换,请从新输入"); i = Console.ReadLine(); } } } #endregion #region 第二题的方法 /// <summary> /// 接收用户输入y/n /// 若是是就返回,不是就从新输 /// </summary> /// <param name="s">接收用户的输入</param> /// <returns></returns> public static string ShuRu(string s) { while (true) { if (s != "y" && s != "n") { Console.WriteLine("只能是y/n"); s = Console.ReadLine(); } else { return s; } } } #endregion #region 第三题的方法 /// <summary> /// 对比两个数的最大值而且返回最大值 /// </summary> /// <param name="n1">第一个整数</param> /// <param name="n2">第二个整数</param> /// <returns>返回的最大值</returns> public static int GetMax(int n1,int n2) { return n1 > n2 ? n1 : n2; } #endregion #region 第四题的方法 /// <summary> /// 计算数组的和并返回 /// </summary> /// <param name="n">传入的数组</param> /// <returns>数组的和</returns> public static int GetSum(int[] n) { int sum = 0; for (int i = 0; i < n.Length; i++) { sum += i; } return sum; } #endregion }
四、out、ref、params
1)、out参数。
若是你在一个方法中,返回多个相同类型的值的时候,能够考虑返回一个数组。
可是,若是返回多个不一样类型的值的时候,返回数组就不行了,那么这个时候,
咱们能够考虑使用out参数。
out参数就侧重于在一个方法中能够返回多个不一样类型的值。递归
class Program { static void Main(string[] args) { //使用方法计算一个数组的最大值,最小值,总和,平均值 int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int[] res = GetMaxMinSumAvg(nums); Console.WriteLine("最大值是{0},最小值是{1},总和是{2},平均{3}", res[0], res[1], res[2], res[3]); Console.ReadKey(); } #region 一次返回多个同类型的值 /// <summary> /// 求一个数组的最大值,最小值,总和,平均值 /// </summary> /// <param name="n">传入的数组</param> /// <returns></returns> public static int[] GetMaxMinSumAvg(int[] n) { int[] res = new int[4]; //假设:res[0]最大值 res[1]最小值 res[2]总和 res[3]平均值 res[0] = n[0]; res[1] = n[0]; res[2] = 0; for (int i = 0; i < n.Length; i++) { if (n[i] > res[0]) { res[0] = n[i]; } if (n[i] < res[1]) { res[1] = n[i]; } res[2] += n[i]; } res[3] = res[2] / n.Length; return res; } #endregion }
out参数的使用内存
class Program { static void Main(string[] args) { //out参数的调用 int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int max1; int min1; int sum1; int avg1; string s1; bool b1; double d1; Test(nums, out max1, out min1, out sum1, out avg1, out s1, out b1, out d1); Console.WriteLine("最大值是{0},最小值是{1},总和是{2},平均{3}", max1, min1, sum1, avg1); Console.WriteLine(s1); Console.WriteLine(b1); Console.WriteLine(d1); Console.ReadKey(); } #region out参数的使用 /// <summary> /// 计算一个数组的最大值,最小值,平均值,总和,在返回一个string类型,bool类型,double类型 /// </summary> /// <param name="n">求值的数值</param> /// <param name="max">最大值</param> /// <param name="min">最小值</param> /// <param name="sum">总和</param> /// <param name="avg">平均值</param> /// <param name="s">返回的string类型</param> /// <param name="b">返回的bool类型</param> /// <param name="d">返回的double类型</param> public static void Test(int[] n,out int max,out int min,out int sum,out int avg, out string s, out bool b, out double d) { //out参数要求在方法的内部必须为其赋值 max = n[0]; min = n[0]; sum = 0; for (int i = 0; i < n.Length; i++) { if (n[i] > max) { max = n[i]; } if (n[i] < min) { max = n[i]; } sum += n[i]; } avg = sum / n.Length; s = "abc"; b = true; d = 3.13; } #endregion }
out参数:方法内必须赋值,方法外不用,由于在方法内已经赋值了字符串
class Program { static void Main(string[] args) { /* 分别提示用户输入用户名和密码,写一个方法来判断用户输入的是否正确 返回给用户一个登录结果,而且还要单独的返回给用户一个登录信息 若是用户名错误,除了返回登良路结果外,还要返回“用户名错误”,密码也同样 */ Console.WriteLine("请输入用户名"); string userName = Console.ReadLine(); Console.WriteLine("请输入密码"); string userPwd = Console.ReadLine(); string msg1; bool b = IsLogin(userName, userPwd, out msg1); Console.WriteLine("登录信息:{0}", b); Console.WriteLine("登录结果:{0}", msg1); Console.ReadKey(); } #region 登录方法练习 /// <summary> /// 判断登录结果 /// </summary> /// <param name="name">用户名</param> /// <param name="pass">密码</param> /// <param name="msg">登录信息</param> /// <returns>返回登录结果</returns> public static bool IsLogin(string name, string pass, out string msg) { if (name == "admin" && pass == "888888") { msg = "登录成功"; return true; } else if (name == "admin") { msg = "密码错误"; return false; } else if (pass == "888888") { msg = "用户名错误"; return false; } else { msg = "全都错误"; return false; } } #endregion } //使用方法实现TryParse class Program { static void Main(string[] args) { Console.WriteLine("请输入一个字符串,转为数字"); string num = Console.ReadLine(); int num1; bool b = TryParse(num, out num1); Console.WriteLine(num1); Console.WriteLine(b); Console.ReadKey(); } public static bool TryParse(string n,out int result) { result = 0; try { result = Convert.ToInt32(n); return true; } catch { return false; } } }
2)、ref参数
可以将一个变量带入一个方法中进行改变,改变完成后,再讲改变后的值带出方法。
ref参数要求在方法外必须为其赋值,而方法内能够不赋值。input
class Program { static void Main(string[] args) { //ref参数调用 int n1 = 10; int n2 = 20; JiaoHuan(ref n1, ref n2); Console.WriteLine(n1); Console.WriteLine(n2); Console.ReadKey(); } public static void JiaoHuan(ref int n1, ref int n2) { int temp = n1; n1 = n2; n2 = temp; } }
3)、params可变参数
将实参列表中跟可变参数数组类型一致的元素都当作数组的元素去处理。
params可变参数必须是形参列表中的最后一个元素。string
class Program { static void Main(string[] args) { // int[] s = { 99, 98, 97 }; Test("张三", 1,99, 98, 97); Console.ReadKey(); } public static void Test(string name,int id, params int[] score) { int sum = 0; for (int i = 0; i < score.Length; i++) { sum += score[i]; } Console.WriteLine("{0}的总成绩是{1},学号{2}", name, sum, id); } } class Program { static void Main(string[] args) { //求任意长度数组的和,整数类型 int[] nums1 = { 1, 2, 3 }; int sum = Nums(1, 2, 3, 4, 5, 6, 7, 8, 9); Console.WriteLine(sum); Console.ReadKey(); } public static void Test(string name,int id, params int[] score) { int sum = 0; for (int i = 0; i < score.Length; i++) { sum += score[i]; } Console.WriteLine("{0}的总成绩是{1},学号{2}", name, sum, id); } public static int Nums(params int[] ns) { int sum = 0; for (int i = 0; i < ns.Length; i++) { sum += ns[i]; } return sum; } }
五、方法的重载
概念:方法的重载指的就是方法的名称相同给,可是参数不一样。
参数不一样,分为两种状况
1)、若是参数的个数相同,那么参数的类型就不能相同。
2)、若是参数的类型相同,那么参数的个数就不能相同。
***方法的重载跟返回值没有关系。it
class Program { static void Main(string[] args) { string n = "姓"; string n1 = "名"; M(n,n1); } public static string M(string name,string waihao) { return name + waihao; } public static void M(int n1, int n2, int n3) { int result = n1 + n2 + n3; } public static double M(double d1, double d2) { return d1 + d2; } }
六、方法的递归
方法本身调用本身。
须要有一个条件,来中止调用
class Program { //定义一个全局字段来做为中止调用的条件,反正方法内,会被重置 public static int i = 0; static void Main(string[] args) { Test(); Console.ReadKey(); } public static void Test() { Console.WriteLine("从前有座山"); Console.WriteLine("山里有座庙"); Console.WriteLine("庙里有一个老和尚跟一个小和尚"); Console.WriteLine("老和尚对小和尚讲故事"); i++; if (i >= 10) { return; } Test(); } }