C#教程
C#控制语句
C#函数
C#数组
C#面向对象
C#命名空间和异常
C#文件IO
C#集合
C#多线程
C#其它

C# 静态类

C# 静态类

C# 静态类与普通类类似,但无法实例化。它只能有静态成员。静态类的优点是它可以保证静态类的实例不能被创建。

C#静态类要记住的要点

C# 静态类只包含静态成员。 无法实例化 C# 静态类。 C# 静态类是密封的。 C# 静态类不能包含实例构造函数。

C#静态类示例

让我们看看包含静态字段和静态方法的静态类示例。
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
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4