惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Secure Thoughts
罗磊的独立博客
T
The Blog of Author Tim Ferriss
人人都是产品经理
人人都是产品经理
博客园 - 叶小钗
Last Week in AI
Last Week in AI
美团技术团队
Google Online Security Blog
Google Online Security Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
D
Docker
G
Google Developers Blog
大猫的无限游戏
大猫的无限游戏
酷 壳 – CoolShell
酷 壳 – CoolShell
小众软件
小众软件
月光博客
月光博客
L
LINUX DO - 最新话题
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
W
WeLiveSecurity
H
Heimdal Security Blog
Vercel News
Vercel News
SecWiki News
SecWiki News
Forbes - Security
Forbes - Security
Blog — PlanetScale
Blog — PlanetScale
Google DeepMind News
Google DeepMind News
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
TaoSecurity Blog
TaoSecurity Blog
T
Troy Hunt's Blog
A
About on SuperTechFans
C
Check Point Blog
S
Security Affairs
Hacker News - Newest:
Hacker News - Newest: "LLM"
AI
AI
WordPress大学
WordPress大学
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Help Net Security
Help Net Security
博客园_首页
The Last Watchdog
The Last Watchdog
S
SegmentFault 最新的问题
Hugging Face - Blog
Hugging Face - Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
Engineering at Meta
Engineering at Meta
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
I
Intezer
K
Kaspersky official blog
M
MIT News - Artificial intelligence
J
Java Code Geeks
G
GRAHAM CLULEY
P
Palo Alto Networks Blog

博客园 - L.Net

sql server不存在或拒绝访问【转】 更新Oracle中的long字段 Ora-28000 the account is locked windows2003与文件共享有关的几个进程 [转]“您试图从目录中执行CGI、ISAPI 或其他可执行程序...” 博文阅读密码验证 - 博客园 博文阅读密码验证 - 博客园 【转】RDLC使用经验 Windows XP .net3.5 环境搭建 showModalDialog数据缓存问题 - L.Net - 博客园 November Report Viewer工具栏显示英文 (转)Reference Equals,==,Equals sql declare声明变量 windows2003计划任务不能启动,"指定的错误是:0x80070005: 拒绝访问" 不可恢复的生成错误 每天知道多一点(二) 还是得继续努力 每天知道多一点
每天知道多一点--[转]静态构造函数
L.Net · 2008-04-18 · via 博客园 - L.Net

类的构造函数有三种:实例构造函数,私有构造函数和静态构造函数.
静态构造函数主要用来初始化静态变量的值.
这种函数只执行一次,在第一次创建类的对象的时候,就会自动调用它.
可以参考一下下面的代码.

 1using System;
 2
 3class Welcome
 4{
 5public Welcome()
 6{
 7Console.WriteLine("构造函数 Welcome()");
 8}

 9
10static Welcome()
11{
12Console.WriteLine("构造函数 static Welcome()");
13}

14
15static void Main()
16{
17new Welcome();
18new Welcome();
19Console.ReadLine();
20}

21}

22
23

执行结果:
构造函数static Welcome()
构造函数Welcome()
构造函数Welcome()

 1public class User
 2  {
 3   static private int count;
 4   static public int Count
 5   {
 6    get
 7    {
 8     return count;
 9    }

10   }

11   public User() 
12   {
13    count++;
14   }

15   static User()
16   {
17    count = 0;
18   }

19  }

20  
21  private void Page_Load(object sender, System.EventArgs e)
22  {
23   // 在此处放置用户代码以初始化页面
24   Response.Write("User Count = " + User.Count);
25   Response.Write("<hr/>Creating User Alex.");
26   User Alex = new User();
27   Response.Write("<br/>User Count = " + User.Count);
28   Response.Write("<hr/>Creating User Rob.");
29   User Rob = new User();
30   Response.Write("<br/>User Count = " + User.Count);
31   Response.Write("<hr/>Creating User Jake.");
32   User Jake = new User();
33   Response.Write("<br/>User Count = " + User.Count);   
34  }

35
36
37

这段代码的执行结果是:
User Count = 0
----------------------------------------------------------------------

----------
Creating User Alex.
User Count = 1
----------------------------------------------------------------------

----------
Creating User Rob.
User Count = 2
----------------------------------------------------------------------

----------
Creating User Jake.
User Count = 3

如果一些操作只希望被执行一次。这时候就可以考虑使用静态构造函数。当然如果设计的面向对象一些的话,那就是使用单件模式了。

静态构造函数可以在第一次调用静态类的任何方法、属性时自动调用,所以在静态构造函数里抛出的异常捕获比较麻烦。

静态构造函数自动被调用,不能被显式调用。虽然提供了许多约束条件,但是静态构造函数执行的确切时间和顺序是不确定的:
一个类的静态构造函数在这个类的任何实例被创建前执行。
一个类的静态构造函数在类的任何静态成员被引用前执行。
一个类的静态构造函数在它的所有派生类的静态构造函数执行之后执行。
一个类的静态构造函数从不会被执行一次以上。

一个类的静态构造函数在它的所有派生类的静态构造函数执行之后执行。

这个是指父类有一个静态构造函数,子类也有一个静态构造函数,比如下面的代码

using System; 

namespace StaticConstruct 


public class ObjectChild:ObjectParent 

public ObjectChild() 

Console.WriteLine(
"子类构造函数!"); 
}
 

static ObjectChild() 

Console.WriteLine(
"子类静态构造函数!"); 
}
 
}
 


public class ObjectParent 

public ObjectParent() 

Console.WriteLine(
"父类构造函数!"); 
}
 

static ObjectParent() 

Console.WriteLine(
"父类静态构造函数!"); 
}
 
}
 

class Class1 

[STAThread] 
static void Main(string[] args) 

ObjectChild obj 
= new ObjectChild(); 
Console.ReadLine(); 
}
 
}
 
}
 

执行的结果是:

子类静态构造函数!
父类静态构造函数!
父类构造函数!
子类构造函数!