黑马程序员————方法简介
2013-08-20 18:52
501 查看
----------------------
ASP.Net+Android+IO开发S、.Net培训、期待与您交流! ----------------------
方法:
方法的声明:
[public] static void 方法名([参数列表])
{
方法体:
}
Public :访问修饰符。
Static:表示静态的。
Void:返回值的类型,如果没有返回值,就写void,如果有返回值,就写对应的类型
方法名:必须符合Pascal命名规范。
参数列表:参数
用[]标记的内容可以省略
注:函数的功能要单一,越单一越好,只用来实现某一项功能。
方法的调用:类名.方法名();
注:如果你写的方法跟Main()方法同在program这个类中,那么你调用方法的时候,可以省略类名,直接方法名()调用。
例1:
例2:
例3:
例4:
Return的两个作用:
1、跳出当前的方法
2、返回要返回的值。
方法之间的调用:
我们在Main()方法中调用Test()方法,我们管Main()方法叫做调用者,管Test()方法叫做被调用者,如果被调用者想得到调用者的值,有两个方法。
1、传参数
2、使用静态字段
注:在静态方法中,只能访问静态字段
如果调用者想要得到被调用者的值,使用返回值:
1、确定有没有返回值
2、如果有返回值,确定返回值的类型
3、Return 返回值
out参数
作用就是返回多个值 在方法内必须为out参数赋值
例1:
例2:
ref 参数
侧重于将值带入一个方法中,再将值从这个方法中带出 ref参数要求在方法外必须赋值
例如:
方法的重载:
方法重载就是指方法的名称相同,参数不同,跟返回值没有关系
如果几个方法构成了重载,如果他们的参数的个数相同,那么它们参数的类型就不能相同,如果他们参数的类型相同,那么它们参数的个数就不能相同
例如:
方法的递归:
就是方法自己调用自己。
例如:
params:
params 构造函数声明数组 而不知道数组长度
注:在方法声明中的 params 关键字之后不允许任何其他参数,并且在方法声明中只允许一个 params 关键字。
例如:
---------------------- ASP.Net+Android+IOS开发、.Net培训、期待与您交流!
----------------------
ASP.Net+Android+IO开发S、.Net培训、期待与您交流! ----------------------
方法:
方法的声明:
[public] static void 方法名([参数列表])
{
方法体:
}
Public :访问修饰符。
Static:表示静态的。
Void:返回值的类型,如果没有返回值,就写void,如果有返回值,就写对应的类型
方法名:必须符合Pascal命名规范。
参数列表:参数
用[]标记的内容可以省略
注:函数的功能要单一,越单一越好,只用来实现某一项功能。
方法的调用:类名.方法名();
注:如果你写的方法跟Main()方法同在program这个类中,那么你调用方法的时候,可以省略类名,直接方法名()调用。
例1:
static void Main(string[] args) { //要求用户输入两个数字 比较这两个数字的大小 Console.WriteLine("请输入第一个数"); int number = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("请输入第二个数"); int number2 = Convert.ToInt32(Console.ReadLine()); int max = GetMax(number, number2); Console.WriteLine("这两个数字中比较大的值是{0}",max); Console.ReadKey(); } /// <summary> /// 比较两个数字的大小,并且返回比较大的值 /// </summary> /// <param name="n1">int 类型的参数</param> /// <param name="n2">int 类型的参数</param> /// <returns>返回的比较大的值</returns> public static int GetMax(int n1, int n2)//形参 { int max = n1 > n2 ? n1 : n2; return max; }
例2:
int[] numbers = { 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 }; int max = GetSumTwo(numbers); Console.WriteLine("这个数组的最大值是{0}", max); Console.ReadKey(); public static int GetSumTwo(int[] nums) { int max = nums[0]; for (int i = 0; i < nums.Length; i++) { if (nums[i] > max) { max = nums[i]; } } return max; }
例3:
Console.WriteLine("请输入一个数字"); string input = Console.ReadLine(); int number = IsNumber(input); bool b = IsPrime(number); Console.WriteLine("您输入的数字{0}质数", b); Console.ReadKey(); public static bool IsPrime(int number) { bool b = true; if (number < 2) { b = false; } for (int i = 2; i <= number - 1; i++) { if (number % i == 0) { b = false; } } return b; }
例4:
static void Main(string[] args) { string[] names = { "张三", "李四", "王二麻子", "赵六", "田七" }; string str = Change(names); Console.WriteLine(str); Console.ReadKey(); } public static string Change(string[] str) { string strNew = ""; for (int i = 0; i < str.Length-1; i++) { strNew += str[i] + "|"; } return strNew + str[str.Length - 1]; }
Return的两个作用:
1、跳出当前的方法
2、返回要返回的值。
方法之间的调用:
我们在Main()方法中调用Test()方法,我们管Main()方法叫做调用者,管Test()方法叫做被调用者,如果被调用者想得到调用者的值,有两个方法。
1、传参数
2、使用静态字段
注:在静态方法中,只能访问静态字段
如果调用者想要得到被调用者的值,使用返回值:
1、确定有没有返回值
2、如果有返回值,确定返回值的类型
3、Return 返回值
out参数
作用就是返回多个值 在方法内必须为out参数赋值
例1:
static void Main(string[] args) { //请用户输入用户名和密码 写一个方法判断用户是否登录成功 并且返回登录信息 Console.WriteLine("请输入用户名"); string strName = Console.ReadLine(); Console.WriteLine("请输入密码"); string strPwd = Console.ReadLine(); string str; bool b = IsLogin(strName, strPwd, out str); Console.WriteLine("登录结果{0}",b); Console.WriteLine("登录信息{0}",str); Console.ReadKey(); } public static bool IsLogin(string name, string pwd,out string msg)//在多余返回值的前面标记out { bool b = false; if(name=="admin"&&pwd=="admin") { b = true; msg = "登录成功"; } else if (name == "admin") { msg = "密码错误"; } else if (pwd == "admin") { msg = "用户名错误"; } else { msg = "未知错误"; } return b; }
例2:
static void Main(string[] args) { // int.TryParse Console.WriteLine("请输入一个数字"); string input = Console.ReadLine(); int res; bool b = MyTryParse(input, out res); Console.WriteLine(b); Console.WriteLine(res); Console.ReadKey(); } public static bool MyTryParse(string input, out int result) { bool b = false; result = 0; try { result = Convert.ToInt32(input); b = true; } catch { } return b; }
ref 参数
侧重于将值带入一个方法中,再将值从这个方法中带出 ref参数要求在方法外必须赋值
例如:
static void Main(string[] args) { int n1 = 10; int n2 = 20; Change(ref n1,ref n2); Console.WriteLine(n1); Console.WriteLine(n2); Console.ReadKey(); } public static void Change 9fcb (ref int number1, ref int number2) { int temp = number1; number1 = number2; number2 = temp; }
方法的重载:
方法重载就是指方法的名称相同,参数不同,跟返回值没有关系
如果几个方法构成了重载,如果他们的参数的个数相同,那么它们参数的类型就不能相同,如果他们参数的类型相同,那么它们参数的个数就不能相同
例如:
public static void GetSum(int n1, int n2) { int sum = n1 + n2; } public static double GetSum(double d1, double d2) { return d1 + d2; } public static string GetSum(string s1, string s2) { return s1 + s2; }
方法的递归:
就是方法自己调用自己。
例如:
public static int i = 0; static void Main(string[] args) { TellStory(); Console.ReadKey(); } public static void TellStory() { Console.WriteLine("从前有座山"); Console.WriteLine("山里有个庙"); Console.WriteLine("庙里有个老和尚和小和尚"); Console.WriteLine("有一天,小和尚哭了,老和尚给小和尚讲故事,讲的是"); i++; if (i >= 10) { return; } TellStory(); }
params:
params 构造函数声明数组 而不知道数组长度
注:在方法声明中的 params 关键字之后不允许任何其他参数,并且在方法声明中只允许一个 params 关键字。
例如:
static void Main(string[] args) { GetSum("张三", 10,50,80,40,05); Console.ReadKey(); } public static void GetSum(string name,int id,params int[] nums) { int sum = 0; for (int i = 0; i < nums.Length; i++) { sum += nums[i]; } Console.WriteLine("{0}的考试成绩的总分是{1},{2}", name, sum,id); }
---------------------- ASP.Net+Android+IOS开发、.Net培训、期待与您交流!
----------------------
相关文章推荐
- 黑马程序员--OC自学笔记---01OC简介、面向对象、类、方法
- 黑马程序员---OC基础---OC简介、OC与C的差异、类和对象与方法
- 黑马程序员——Math方法简介
- 黑马程序员--Foundation框架下三大类简介和基本方法
- 黑马程序员——基础学习---io简介
- 黑马程序员-Java_模版方法设计模式(Template Pattern)
- 黑马程序员——Java学习总结:IO常用方法练习
- ccp系列方法简介
- 黑马程序员——java基础拾遗之IO流(一) 常用类及方法
- H.264/AVC中CAVLC编码方法简介
- BOM对象以及方法极简介
- 黑马程序员---description方法(NSLog输出)
- Java中的StringTokenizer类的简介使用方法
- Jakarta Commons Digester 简介和具体使用方法
- iOS中常用的四种数据持久化方法简介
- Python中encode()方法的使用简介
- 在VC++6.0下设置GDI+(最简介的方法)(转)
- 黑马程序员-----构造方法-String
- 黑马程序员_Java基础_面向对象(多态、Object类相关方法)