第9章 - 方法
嗨,朋友!我是长安。
前面的代码都写在 Main 方法里,如果代码很多就会很乱。这一章我们要学习如何创建自己的方法,让代码更有条理!
🤔 什么是方法?
方法就是一段完成特定功能的代码块,可以重复调用。
类比:就像一个工具箱里的工具,每个工具都有特定的功能,需要时拿出来用。
🌟 方法的基本结构
访问修饰符 返回类型 方法名(参数列表)
{
// 方法体
return 返回值; // 如果有返回值
}
示例
// 无参数无返回值
static void SayHello()
{
Console.WriteLine("Hello!");
}
// 有参数无返回值
static void Greet(string name)
{
Console.WriteLine($"你好,{name}!");
}
// 有参数有返回值
static int Add(int a, int b)
{
return a + b;
}
// 调用方法
static void Main(string[] args)
{
SayHello(); // 调用无参数方法
Greet("张三"); // 调用有参数方法
int result = Add(5, 3); // 调用有返回值方法
Console.WriteLine(result); // 8
}
📖 方法的参数
值参数(默认)
static void ChangeValue(int x)
{
x = 100;
Console.WriteLine($"方法内:{x}"); // 100
}
static void Main(string[] args)
{
int num = 10;
ChangeValue(num);
Console.WriteLine($"方法外:{num}"); // 10(不变)
}
ref 参数(引用传递)
static void ChangeValue(ref int x)
{
x = 100;
}
static void Main(string[] args)
{
int num = 10;
ChangeValue(ref num);
Console.WriteLine(num); // 100(改变了)
}
out 参数(输出参数)
static void Calculate(int a, int b, out int sum, out int product)
{
sum = a + b;
product = a * b;
}
static void Main(string[] args)
{
int sum, product;
Calculate(5, 3, out sum, out product);
Console.WriteLine($"和:{sum},积:{product}");
}
params 参数(可变参数)
static int Sum(params int[] numbers)
{
int total = 0;
foreach (int num in numbers)
{
total += num;
}
return total;
}
static void Main(string[] args)
{
Console.WriteLine(Sum(1, 2, 3)); // 6
Console.WriteLine(Sum(1, 2, 3, 4, 5)); // 15
}
🎯 方法重载
同名方法,但参数不同(个数或类型)。
static int Add(int a, int b)
{
return a + b;
}
static double Add(double a, double b)
{
return a + b;
}
static int Add(int a, int b, int c)
{
return a + b + c;
}
static void Main(string[] args)
{
Console.WriteLine(Add(1, 2)); // 3
Console.WriteLine(Add(1.5, 2.5)); // 4.0
Console.WriteLine(Add(1, 2, 3)); // 6
}
💡 实战示例
示例1:计算器方法
using System;
namespace CalculatorMethods
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Add(10, 5)); // 15
Console.WriteLine(Subtract(10, 5)); // 5
Console.WriteLine(Multiply(10, 5)); // 50
Console.WriteLine(Divide(10, 5)); // 2
Console.ReadKey();
}
static double Add(double a, double b)
{
return a + b;
}
static double Subtract(double a, double b)
{
return a - b;
}
static double Multiply(double a, double b)
{
return a * b;
}
static double Divide(double a, double b)
{
if (b == 0)
{
Console.WriteLine("错误:除数不能为0");
return 0;
}
return a / b;
}
}
}
示例2:数组操作方法
using System;
namespace ArrayMethods
{
class Program
{
static void Main(string[] args)
{
int[] numbers = { 5, 2, 9, 1, 7, 3 };
PrintArray(numbers);
Console.WriteLine($"最大值:{GetMax(numbers)}");
Console.WriteLine($"最小值:{GetMin(numbers)}");
Console.WriteLine($"平均值:{GetAverage(numbers):F2}");
Console.ReadKey();
}
// 打印数组
static void PrintArray(int[] arr)
{
Console.Write("数组元素:");
foreach (int num in arr)
{
Console.Write(num + " ");
}
Console.WriteLine();
}
// 获取最大值
static int GetMax(int[] arr)
{
int max = arr[0];
foreach (int num in arr)
{
if (num > max) max = num;
}
return max;
}
// 获取最小值
static int GetMin(int[] arr)
{
int min = arr[0];
foreach (int num in arr)
{
if (num < min) min = num;
}
return min;
}
// 获取平均值
static double GetAverage(int[] arr)
{
int sum = 0;
foreach (int num in arr)
{
sum += num;
}
return (double)sum / arr.Length;
}
}
}
📝 本章小结
- 方法可以封装代码,提高复用性
- 方法有参数和返回值
ref和out可以改变参数值params可以接收可变数量的参数- 方法重载允许同名不同参
- 好的方法名应该描述功能
🎯 下一步
掌握了方法后,下一章我们要开始学习面向对象编程的基础概念!
💪 练习题
- 创建一个判断质数的方法
- 创建一个反转字符串的方法
- 创建一个计算阶乘的方法
- 创建一个判断闰年的方法
答案提示
static bool IsPrime(int num)static string Reverse(string str)static long Factorial(int n)static bool IsLeapYear(int year)
