📜  C#|静态类

📅  最后修改于: 2021-05-29 19:29:42             🧑  作者: Mango

在C#中,允许使用static关键字创建一个静态类。静态类只能包含静态数据成员,静态方法和静态构造函数,不允许创建静态类的对象。静态类是密封的,这意味着您不能从另一个类继承静态类

句法:

static class Class_Name
{

      // static data members 
     // static method
}

在C#中,静态类包含两种类型的静态成员,如下所示:

  • 静态数据成员:由于静态类始终包含静态数据成员,因此使用static关键字声明静态数据成员,并使用类名直接访问它们。静态数据成员的内存是单独分配的,与对象没有任何关系。

    Synatx:

    static class Class_name 
    {
        public static nameofdatamember;
    }
    
  • 静态方法:由于静态类始终包含静态方法,因此使用static关键字声明静态方法。这些方法仅访问静态数据成员,而不能访问非静态数据成员。

    Synatx:

    static class Class_name {
    
        public static nameofmethod()
        {
             // code 
         }
    }
    

范例1:

// C# program to illustrate the 
// concept of static class
using System;
  
namespace ExampleOfStaticClass {
  
// Creating static class
// Using static keyword
static class Author {
  
    // Static data members of Author
    public static string A_name = "Ankita";
    public static string L_name = "CSharp";
    public static int T_no = 84;
  
    // Static method of Author
    public static void details()
    {
        Console.WriteLine("The details of Author is:");
    }
}
  
// Driver Class
public class GFG {
  
    // Main Method
    static public void Main()
    {
  
        // Calling static method of Author
        Author.details();
  
        // Accessing the static data members of Author
        Console.WriteLine("Author name : {0} ", Author.A_name);
        Console.WriteLine("Language : {0} ", Author.L_name);
        Console.WriteLine("Total number of articles : {0} ", 
                                              Author.T_no);
    }
}
}
输出:
The details of Author is:
Author name : Ankita 
Language : CSharp 
Total number of articles : 84

范例2:

// C# program to demonstrate 
// the concept of static class
using System;
  
  
// declaring a static class
public static class GFG {
      
    // declaring static Method
    static void display()
    {
        Console.WriteLine("Static Method of class GFG");
    }
      
}
  
// trying to inherit the class GFG
// it will give error as static 
// class can't be inherited 
class GFG2 : GFG {
      
    public static void Main(String[] args) {
          
          
    }
}

编译时错误:

说明:在上面的示例中,我们使用static关键字将一个静态类命名为AuthorAuthor类包含名为A_nameL_nameT_no的静态数据成员,以及名为details()的静态方法。可以通过使用其类名如Author.details();来简单地调用静态类的方法 。我们知道静态类不包含对象,因此可以通过其类名访问Author类的数据成员,例如Author.A_nameAuthor.L_nameAuthor.T_no

静态和非静态类之间的区别

Static Class Non-Static Class
Static class is defined using static keyword. Non-Static class is not defined by using static keyword.
In static class, you are not allowed to create objects. In non-static class, you are allowed to create objects using new keyword.
The data members of static class can be directly accessed by its class name. The data members of non-static class is not directly accessed by its class name.
Static class always contains static members. Non-static class may contain both static and non-static methods.
Static class does not contain an instance constructor. Non-static class contains an instance constructor.
Static class cannot inherit from another class. Non-static class can be inherited from another class.