20250529-C#知识:静态类、静态构造函数和拓展方法
C#知识:静态类、静态构造函数和拓展方法
静态类一般用来编写工具类
1、静态类
- 用static关键字修饰的类
- 一般充当工具类
- 只能包含静态成员,不能包含静态索引器
- 不能被实例化
- 静态方法只能使用静态成员
- 非静态方法既可以使用非静态成员,也可以使用静态成员
static class Tools{static private string name; //静态成员变量public static string[] versions;public static Dog toolDog; //非静态类型的静态成员变量static public int Count => versions.Length; //静态属性//static public int this[int idx]//{//}static Tools() //静态构造{name = "驴子牌工具包";versions = new string[]{ "1.1", "2.1", "3.5"};toolDog = new Dog("波奇"); //非静态类型实例化Console.WriteLine("静态类构造被调用!");}public static string GetDogName() => toolDog.littleName; //静态成员方法 }
2、静态构造函数
- 加了static关键字修饰的构造函数
- 可以出现在静态类和非静态类中
- 不能有访问修饰符
- 不能有参数
- 只会调用一次
- 静态类中的静态构造一般在其成员被使用之前被自动调用
- 非静态类的静态构造一般在实例化对象或使用静态成员之前被自动调用
- 一般用来初始化静态成员变量
static Tools() //静态类中的静态构造函数{name = "驴子牌工具包";versions = new string[]{ "1.1", "2.1", "3.5"};toolDog = new Dog("波奇"); //非静态类型实例化Console.WriteLine("静态类中的静态类构造被调用!");}
static Dog() //非静态类中的静态构造函数{name = "布莱恩";Console.WriteLine("非静态类中的静态类构造被调用!");}
3、拓展方法
- 为非静态类添加方法
- 形式上是静态类中的静态方法
- 语法 访问修饰符 static 返回值 拓展方法名(this 要拓展的类 使用拓展方法的实例化对象, 参数类型 参数1, 参数类型 参数2,…)
- 好处方便给封装好的类添加新的方法,不用修改类代码,不用写一个继承类
//私有拓展private static void Speak2(this Dog dog) => Console.Write("喵喵喵");//公有拓展public static void Speak3(this Dog dog, string word){Console.Write("{0}:", Dog.name); //非静态方法使用静态成员变量dog.Speak1();dog.Speak2(); //调用另外一个拓展方法Console.WriteLine(word);}//为float类型拓展方法public static void MyFunc(this float target) => Console.WriteLine("这是为float拓展的方法");
4、静态成员变量 VS const成员变量
- const成员变量必须初始化,静态成员变量不需要
- const只能修饰变量
- const关键字只能位于访问修饰符后面
5、完整代码示例:
using System.Runtime.CompilerServices;namespace LearnStatic
{static class Tools{static private string name; //静态成员变量public static string[] versions;public static Dog toolDog; //非静态类型的静态成员变量static public int Count => versions.Length; //静态属性//static public int this[int idx]//{//}static Tools() //静态类中的静态构造函数{name = "驴子牌工具包";versions = new string[]{ "1.1", "2.1", "3.5"};toolDog = new Dog("波奇"); //非静态类型实例化Console.WriteLine("静态类中的静态类构造被调用!");}public static string GetDogName() => toolDog.littleName; //静态成员方法//私有拓展private static void Speak2(this Dog dog) => Console.Write("喵喵喵");//公有拓展public static void Speak3(this Dog dog, string word){Console.Write("{0}:", Dog.name); //非静态方法使用静态成员变量dog.Speak1();dog.Speak2(); //调用另外一个拓展方法Console.WriteLine(word);}//为float类型拓展方法public static void MyFunc(this float target) => Console.WriteLine("这是为float拓展的方法");}class Dog{public static string name;public string littleName;public Dog() => Console.WriteLine("非静态类的无参构造被调用");public Dog(string name) => this.littleName = name;static Dog() //非静态类中的静态构造函数{name = "布莱恩";Console.WriteLine("非静态类中的静态类构造被调用!");} public void Speak1() => Console.Write("旺旺旺");}internal class Program{static void Main(string[] args){//Console.WriteLine(Dog.name); //非静态类中的静态类构造被调用! 布莱恩//Console.WriteLine("*************");Dog dog = new Dog(); //非静态类中的静态类构造被调用! 非静态类的无参构造被调用Console.WriteLine("*************");dog.Speak1(); //旺旺旺Console.WriteLine("*************");//调用拓展方法,拓展方法是非静态方法dog.Speak3("你喜欢我写的小说吗?"); //静态类中的静态类构造被调用!//布莱恩:旺旺旺喵喵喵你喜欢我写的小说吗?Console.WriteLine(Tools.toolDog.littleName); //波奇float f1 = 0.36f;f1.MyFunc(); //这是为float拓展的方法}}
}
6、参考资料
- 《唐老狮C#》
本篇结束,感想您的阅读~