Files
My-Notes/笔记/CSharp/核心/静态成员.md
T
2026-06-18 20:01:30 +08:00

51 lines
1.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
概念:
用static 修饰的成员变量、方法、属性
特点:
==直接用类名点出来使用 没有实例化!!!!==
通常用法:
PI之类的全局且唯一性的
语法:
static 变量名 = XXX
案例:
```Csharp
class Test
{
static public float PI = 3.1415926; //静态成员
public int testInt = 100;
public static float CalcCircle(float r) //静态方法
{
return PI * r;
}
public void TestFunc() //非静态方法
{
Console.WriteLine("123");
}
}
static void Main(string[] args)
{
Console.WriteLine(Test.PI);
Console.WriteLine(Test.CalcCircle(2));
}
```
注意:
和程序同生共死,在程序使用后永远不会清除内存,直到程序结束
非静态函数可以调用静态成员
静态成员声明后可以不赋初始值
静态方法中不能使用==没有实例化的==非静态成员
[[常量]]可以理解为特殊的静态
共同点
- 都可以通过类名点出来使用
不同点
- const必须初始化,不能修改 static 没有这个限制
- const值能修饰变量、static可以修饰很多
- const一定是写在访问修饰符后面的,static没有这个要求