C# static 关键字:静态变量、方法与类完全掌握
C# static 关键字
本指南讲解 C# static 关键字,涵盖静态变量、方法、类,配以清晰实战示例。
在 C# 中,对成员应用 static 关键字将为该类型创建单个共享实例。每个对象不再持有自己的副本,静态成员每类型仅存在一份。
C# 静态变量
字段声明为 static 后,可直接通过类名访问,无需创建对象。
using System;
namespace StaticKeyword {
class Student {
// 所有学生共享的静态变量
public static string department = "Computer Science";
}
class Program {
static void Main(string[] args) {
// 通过类名访问静态字段
Console.WriteLine("Department: " + Student.department);
Console.ReadLine();
}
}
}
输出
Department: Computer Science
department 为所有 Student 实例共享,故以 Student.department 访问。
静态变量 vs 实例变量
实例变量属于各个对象,每对象一份副本。例如:
class Student {
public string studentName;
}
class Program {
static void Main(string[] args) {
Student s1 = new Student();
Student s2 = new Student();
}
}
s1 和 s2 拥有各自的 studentName。相比之下,静态字段为所有实例共享,且无需创建对象即可访问。
示例:静态与实例变量对比
using System;
namespace StaticKeyword {
class Student {
public static string schoolName = "Programiz School"; // 共享
public string studentName; // 独有
}
class Program {
static void Main(string[] args) {
Student s1 = new Student();
s1.studentName = "Ram";
Console.WriteLine("Name: " + s1.studentName); // 实例
Console.WriteLine("School: " + Student.schoolName); // 静态
Student s2 = new Student();
s2.studentName = "Shyam";
Console.WriteLine("Name: " + s2.studentName);
Console.WriteLine("School: " + Student.schoolName);
Console.ReadLine();
}
}
}
输出
Name: Ram School: Programiz School Name: Shyam School: Programiz School
静态字段 schoolName 对所有学生相同,节省内存且逻辑一致。
C# 静态方法
静态方法在类上调用,而非实例。只能访问其他静态成员。
class Test {
public static void Display() {
// 实现
}
}
class Program {
static void Main(string[] args) {
Test.Display();
}
}
Test 的所有对象共享同一个 Display 方法,故通过类名调用。
示例:静态与非静态方法
using System;
namespace StaticKeyword {
class Test {
public void Display1() {
Console.WriteLine("Non static method");
}
public static void Display2() {
Console.WriteLine("Static method");
}
}
class Program {
static void Main(string[] args) {
Test t1 = new Test();
t1.Display1(); // 实例调用
Test.Display2(); // 静态调用
Console.ReadLine();
}
}
}
输出
Non static method Static method
注意:Main 方法本身是 static,故无需实例即可运行。
静态类
静态类不可实例化,只能包含静态成员。适合归类辅助方法。
using System;
namespace StaticKeyword {
static class Test {
static int a = 5;
static void Display() {
Console.WriteLine("Static method");
}
static void Main(string[] args) {
// Test t1 = new Test(); // ❌ 编译错误
Console.WriteLine(a);
Display();
}
}
}
尝试实例化 Test 将报 CS0723 和 CS0712 错误,确认其静态性质。
静态类不可继承。例如:
static class A { /* ... */ }
// class B : A { /* ... */ } // ❌ 错误
类内部访问静态成员
同一类内,可直接引用静态字段和方法,无需类名限定。
using System;
namespace StaticKeyword {
class Test {
static int age = 25;
public static void Display() {
Console.WriteLine("Static method");
}
static void Main(string[] args) {
Console.WriteLine(age); // 直接访问
Display(); // 直接访问
Console.ReadLine();
}
}
}
输出
25 Static method
此处 age 和 Display 均未加 Test. 前缀即可使用。
C语言
- C# 类与对象:面向对象编程的基石
- 理解 C# 访问修饰符:public、private、protected、internal 等完全指南
- 掌握 C# this 关键字:消歧义、构造函数链、传递实例与索引器
- 掌握 C# 抽象类与抽象方法:实战指南
- 理解 C# 嵌套类:定义、访问与继承实战
- 理解 C# 分部类与分部方法
- 掌握 C# sealed 类与方法:用法、示例与代码安全性能提升
- C++多态实战指南:虚函数、动态绑定与纯虚函数
- C++数据抽象实战:接口与实现分离的设计艺术
- C++数据封装:守护代码安全的核心机制
- C# 程序结构解析:从 Hello World 看命名空间、类、Main 入口与编译运行
- Java static 详解:静态变量、方法、代码块与实战示例