C# Static静态类与普通类类似,但不能实例化。静态类的优点是保证您不会创建静态类的实例。
让无涯教程看看静态类的示例,它包含静态字段和静态方法。
链接:https://www.learnfk.comhttps://www.learnfk.com/csharp/c-sharp-static-class.html
来源:LearnFk无涯教程网
using System; public static class MyMath { public static float PI=3.14f; public static int cube(int n){return n*n*n;} } class TestMyMath{ public static void Main(string[] args) { Console.WriteLine("Value of PI is: "+MyMath.PI); Console.WriteLine("Cube of 3 is: " + MyMath.cube(3)); } }
输出:
Value of PI is: 3.14 Cube of 3 is: 27
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)