C#方法构建的简单介绍

到这里,我们学习了C#一些常见的,重要的基本元素,目前已经够用了,在以后的学习中我们在逐渐往里添加。在前几节的

学习中,我们稍微的渗透了一些方法的相关信息,那么现在我们着重介绍一下方法。

C#方法也叫函数,我们可以继续用类比的方式去理解方法,相信有过工控经验的童鞋们,肯定直到FB,FC块的概念吧,

其实C#也一样,也类似与我们的静态方法和非静态方法,也需要我们对其形参进行实例化。他们的产生或者作用和PLC一样

也是一种重用机制。

方法

概念:方法表示这个对象能够做什么,也就是封装了这个对象的行为。

类型:实例方法→静态方法→(抽象方法,虚方法)→特殊的:构造方法(对象创建的时候使用)

语法:

[public] static 返回值类型 方法名([参数列表])

{

    //这里编写方法的主体(功能实现的具体过程)

    方法体;

    return 返回值;//如果没有返回值,则不需要写该语句

}

调用规范:对象名.方法名(参数1,参数2,。。。) 静态方法

方法名定义:一般是“动词”或者“动宾短语”,采用Pascal命名法,首字母大写,不能已数字开头。

 

public:访问修饰符,公开公共的,那都可以访问。

static:静态的

返回值类型:如果不需要返回值,写void

参数列表:完成这个方法所必须要提供给这个方法的条件。如果没有参数,()也不能省略。

return的作用:1.在方法中返回要返回的值。

              2.立即结束本次方法。

方法写好后,一定要在Main中调用,是不是和PLC很像呢。

语法:类名.方法名([参数])

如果你写的方法和Main函数同在一个program中类名可以省略。

如果被调用者想要得到调用者的值:

1)传递参数。

2)使用静态字段来模拟全局变量。

如果调用者想要得到被调用者的值

1)返回值

无论实参还是形参,都是在内存那种开辟了空间了。

我们在构建方法时,一定要使方法单一化。方法中最忌讳的就是出现提示用户的字眼。

我们看一个练习:读取输入的整数,定义成方法,如果用户输入的是数字则返回,否则重新输入。

 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 草稿
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             Console.WriteLine("请输入一个数字");
14             string input = Console.ReadLine();
15             int number = GetNumber(input);
16             Console.WriteLine(number);
17             Console.ReadKey();
18         }
19 
20         public static int GetNumber(string s)
21         {
22             while (true)
23             {
24                 try
25                 {
26                     int number = Convert.ToInt32(s);
27                     return number;
28                 }
29                 catch 
30                 {
31 
32                     Console.WriteLine("请重新输入");
33                     s = Console.ReadLine();
34                 }
35             }
36         }
37     }
38 }

 

其实综合前几个章节的讲述,我不难发现死循环不是一无是处的,有时候在循环问答的时候还是很有帮助的。

我们继续夯实一下:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 草稿
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             Console.WriteLine("请输入yes或者no");
14             string str = Console.ReadLine();
15             string answer = IsYesOrNo(str);
16             Console.ReadKey();
17         }
18 
19         public static string IsYesOrNo(string input)
20         {
21             while (true)
22             {
23                 if (input == "yes" || input == "no")
24                 {
25                     return input;
26                 }
27                 else
28                 {
29                     Console.WriteLine("只能输入yes或者no,请重新输入");
30                 }
31                
32             }
33         }
34     }
35 }

下面我们在研究一下关于方法的三个高级参数:

(1)out参数

如果在同一个方法中,返回多个同类型的的值的时候,可以考虑返回一个数组,但是,如果返回多个不同类型的值的时候,

返回数组就不行了,那么这个时候我们可以考虑out参数。

out参数就侧重于在一个方法中可以返回多个不同类型的值。(当然也可以是多个相同值)

 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 草稿
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             int[] numbers = { 1,2,3,4,5,6,7,8,9,0};
14             int max = 0;
15             int min = 0;
16             int sum = 0;
17             int avg = 0;
18 
19             Test(numbers,out max,out min,out sum,out avg);
20             Console.WriteLine(max);
21             Console.WriteLine(min);
22             Console.WriteLine(sum);
23             Console.WriteLine(avg);
24             Console.ReadKey();
25         }
26 
27         public static void Test(int[] nums,out int max,out int min,out int sum,out int avg)
28         {
29             max = nums[0];
30             min = nums[0];
31             sum = 0;
32             for (int i = 0; i < nums.Length; i++)
33             {
34                 if (nums[i] > max)
35                 {
36                     max = nums[i];
37                 }
38                 if (nums[i] < min)
39                 {
40                     min = nums[i];
41                 }
42                 sum += nums[i];
43             }
44             avg = sum / nums.Length;
45         }
46     }
47 }

 

练习:提示用户输入用户名和密码,写一个方法判断用户输入是否正确。返回用户一个登陆结果和一个登录信息。

      如果用户名错误,除了返回登陆结果之外,还要返回一个“登录错误”。密码错误,返回“密码错误”。

 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 草稿
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             Console.WriteLine("请输入用户名");
14             string userName = Console.ReadLine();
15             Console.WriteLine("请输入密码");
16             string userPwd = Console.ReadLine();
17             string msg;
18             bool b = IsLogin(userName,userPwd,out msg);
19             Console.WriteLine(b);
20             Console.WriteLine(msg);
21             Console.ReadKey();
22         }
23 
24         public static bool IsLogin(string name,string pwd,out string msg)
25         {
26             if (name == "admin" && pwd == "888888")
27             {
28                 msg = "登录成功";
29                 return true;
30             }
31             else if (name == "admin")
32             {
33                 msg = "密码错误";
34                 return false;
35             }
36             else if (pwd == "888888")
37             {
38                 msg = "用户名错误";
39                 return false;
40             }
41             else
42             {
43                 msg = "未知错误";
44                 return false;
45             }
46         }
47     }
48 }

 我们发现我们再用out参数的时候,一定要在方法内部赋值。因为值是从方法内部传出去的。

(2)ref参数

 

能够将一个变量带入一个方法中进行改变,改变完成后,再将改变后的值带出方法。(非常类似与return),而且需要从方法的外部赋值。

ref的作用就是将参数带入到方法内部,再把值带出。所以ref参数要求方法外必须赋值,而方法内可以不赋值。

(3)params可变参数

将实参列表中跟可变参数数组类型一致的元素都当作数组的元素去处理。必须是形参列表中最后一个参数。

 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace Panel
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             Test("张三",99,88,77);
14 
15             Console.ReadKey();
16         }
17 
18         public static void Test(string name,params int[] score)
19         {
20             int sum = 0;
21             for (int i = 0; i < score.Length; i++)
22             {
23                 sum += score[i];
24             }
25 
26             Console.WriteLine("{0}这次考试的总成绩是{1}",name,sum);
27         }
28     }
29 }

 


方法的重载

方法重载的好处

1.减少类的对外接口(只显示一个方法),降低类的复杂度。

2.便于用户使用(相同功能的方法名称一样)和识别。

方法的重载指的就是方法的名称相同,但是参数不同。方法的重载和返回值没有关系。

(1)如果参数的个数相同,那么参数的类型就不能相同。

(2)如果参数的类型相同,那么参数的个数就不能相同。

静态成员使用经验

1.静态成员在程序运行时被调入内存中,并且在系统未关闭之前不会被GC回收。

2.类的成员使用非常频繁时候,可以考虑使用static修饰,但是不要使用过多。

3.静态成员不能直接调用实例成员(静态方法不能直接调用实例方法)。

4.静态方法也可以重载

 

方法的递归

方法自己调用自己。 

 

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace Panel
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             Console.WriteLine("请输入第一个数字");
14             string strNumberOne = Console.ReadLine();
15             int numberOne = GetNumber(strNumberOne);
16             Console.WriteLine("请输入第二个数字");
17             string strNumberTwo = Console.ReadLine();
18             int numberTwo = GetNumber(strNumberOne);
19 
20             JudgeNumber(ref numberOne,ref numberTwo);
21             int sum = GetSum(numberOne,numberTwo);
22             Console.WriteLine(sum);
23             Console.ReadKey();
24 
25         }
26 
27         public static int GetNumber(string s)
28         {
29             while (true)
30             {
31                 try
32                 {
33                     int number = Convert.ToInt32(s);
34                     return number;
35                 }
36                 catch 
37                 {
38 
39                     Console.WriteLine("输入有误");
40                     s = Console.ReadLine();
41                 }
42                 
43             }
44         }
45 
46         public static void JudgeNumber(ref int n1,ref int n2)
47         {
48             while (true)
49             {
50                 if (n1 < n2)
51                 {
52                     return;
53                 }
54                 else
55                 {
56                     Console.WriteLine("第一个数字不能大于或等于第二个数字,青虫滚输入第一个数字");
57                     string s1 = Console.ReadLine();
58 
59                     n1 = GetNumber(s1);
60                     Console.WriteLine("请输入第二个数字");
61                     string s2 = Console.ReadLine();
62                     n2 = GetNumber(s2);
63                 }
64             }
65         }
66 
67         public static int GetSum(int n1,int n2)
68         {
69             int sum = 0;
70             for (int i = n1; i <= n2; i++)
71             {
72                 sum += i;
73             }
74             return sum;
75         }
76     }
77 }




posted @ 2019-12-28 14:25  大勇者  阅读(1271)  评论(0编辑  收藏  举报