Static是C#中的關鍵字,使用該關鍵字無法實例化項目,這意味著使用static關鍵字的項目將變得不可實例化,並且它可以應用於類別、變數、方法和建構函數,從而創建靜態類別、靜態變數、靜態方法、靜態建構子和靜態成員。可以在成員的聲明中使用關鍵字static來創建,不屬於特定類型的物件;相反,它屬於類型本身。
任何成員都可以透過在成員宣告之前使用關鍵字 static 來宣告為靜態成員。讓我們了解如何宣告靜態類別、靜態變數、靜態方法和靜態建構子。
可以使用關鍵字 static 將類別宣告為靜態類別。靜態類別中只能包含靜態資料成員、靜態方法和靜態建構子。無法建立靜態類別的物件。靜態類別中不能進行繼承。
讓我們透過下面的程式來宣告並理解靜態類別:
代碼:
using System; //A static class is created using the keyword static static class example { // Creating a string variable and declaring it as static using static keyword public static string demo = "Welcome to C#"; } //Creating a public class public class check { // Calling the static main method static public void Main() { //The static member of the tutorial class which is static is called Console.WriteLine("Understanding static keyword : {0} ", example.demo); } }
上述程式的輸出如下圖:
輸出:
可以使用關鍵字 static 將變數宣告為靜態變數。當我們將變數宣告為靜態變數並與所有類別物件共用時,就會建立該變數的單一副本。我們使用類別的名稱來存取靜態變數;不需要任何物件來存取靜態變數。
讓我們透過下面的程式來宣告並理解靜態變數:
代碼:
using System; //A static class is created using the keyword static class check { // Creating a string variable and declaring it as static using static keyword public static string learn = "Python"; } //Creating a public class public class checkvar { // Calling the static main method static public void Main() { //The static member of the check class which is static is called Console.WriteLine("I want to learn : {0} ", check.learn); } }
上述程式的輸出如下圖:
輸出:
可以使用關鍵字 static 將方法宣告為靜態方法。我們使用類別的名稱來存取靜態方法;靜態和非靜態欄位都可以使用靜態方法存取。存取靜態欄位不需要物件或類別名,而存取非靜態欄位則需要物件。
讓我們透過下面的程式來宣告並理解靜態方法:
代碼:
using System; //A static class is created using the keyword static class students { // Creating a string variable and declaring it as static using static keyword static public int number = 100; //Creating a method and declaring it as static using static keyword public static void count() { Console.WriteLine("Number of students"+ " learning python is :{0}", number); } } //Creating a public class public class display { // Calling the static main method static public void Main() { //class name is used to access the number of students students.count(); } }
上述程式的輸出如下圖:
輸出:
可以使用關鍵字 static 將建構子宣告為靜態建構子。靜態建構函數會在建構函數的實例運行之前自動調用,並且僅在程式中引用的類別 id 之前的類別中調用一次。類別的名稱必須與建構函數的名稱相同。
讓我們透過下面的程式來宣告並理解靜態建構子:
代碼:
using System; //A public class is created public class constructor { //A static constructor is defined whose name is same as the name of the class static constructor() { //This statement is displayed as the first line of the output and it is executed only once Console.WriteLine("Understanding Static Constructor"); } // The constructor instance is created public constructor(int a) { Console.WriteLine("constructor instance " + a); } // A public method is defined public string details(string name, string work) { return "Name: " + name + " Work: " + work; } // Calling the public main method public static void Main() { // Invoking all the constructors defined before constructor con = new constructor(1); Console.WriteLine(con.details("Shobha", "Data Scientist")); //Again Invoking all the constructors defined before constructor co = new constructor(2); Console.WriteLine(co.details("Shobha", "Tyson")); } }
上述程式的輸出如下圖:
輸出:
靜態類別與非靜態類別類似,但靜態類別不能建立實例;也就是說,我們不能使用new運算子建立靜態類別的變數或對象,並且使用類別名稱本身來存取靜態類別的成員。例如,考慮 .NET 框架中的靜態類別 System.math;它由執行數學運算所需的方法組成,無需建立數學類別的實例。
C# 程式來說明靜態類別、靜態變數、靜態方法和靜態建構子。
代碼:
using System; //A public static class is created public static class check { //A public static variable is created public static int height; //A static constructor is created static check() { // Method is called by the constructor height = distance(20); } //A static method is created static int distance(int number) { return Environment.TickCount * number; } } //A public class is created public class Program { public static void Main() { //Accessing the constructor and its object Console.WriteLine("Bird can fly upto: {0}", check.height); } }
上述程式的輸出如下圖:
輸出:
以上是C# 中的靜態關鍵字的詳細內容。更多資訊請關注PHP中文網其他相關文章!