涉及知识点: C#扩展方法
扩展方法案例
使用场景: 在不修改源码的情况下为其他类型增加功能 或 增加方法
添加扩展方法注意点:
1、静态类里的静态方法
2、需要添加的方法入参加(this 类名 变量名)
代码编写:
internal class Program
{
static void Main(string[] args)
{
Student Tom = new Student();
Tom.Art();
Tom.Music();
Console.WriteLine("Hello World!");
}
}
class Student
{
public void Music() => Console.WriteLine("听音乐");
}
static class StudentExtend
{
public static void Art(this Student s) => Console.WriteLine("画画");
}
结果显示:
如有错误,烦请批评指正