靜態(tài)構(gòu)造函數(shù):
?1.一定是靜態(tài)類才有靜態(tài)構(gòu)造函數(shù)嗎?錯(cuò)的
?靜態(tài)構(gòu)造函數(shù)的特點(diǎn):
?1.靜態(tài)構(gòu)造函數(shù)沒有修飾符修飾(默認(rèn)修飾符是private,但是不能寫出來)
?2.靜態(tài)構(gòu)造函數(shù)是系統(tǒng)默認(rèn)調(diào)用罕邀,程序員無法手動(dòng)調(diào)用
?3.如果類中沒有靜態(tài)構(gòu)造函數(shù)乏苦,而此時(shí)類中又包含靜態(tài)字段(未初始化的)株扛,那么此時(shí)編譯器會(huì)默認(rèn)生成靜態(tài)構(gòu)造函數(shù)
?4.在靜態(tài)類中或者靜態(tài)方法中,不能使用實(shí)例對(duì)象汇荐。
?5.如果類中包含靜態(tài)成員洞就,且沒有被初始化,系統(tǒng)會(huì)默認(rèn)提供一個(gè)默認(rèn)的值掀淘,這也間接的說明了我們不可以直接調(diào)用靜態(tài)構(gòu)造函數(shù)旬蟋,也沒有辦法 ? ? 控制靜態(tài)函數(shù)的執(zhí)行時(shí)間
?6.靜態(tài)構(gòu)造函數(shù)只能被調(diào)用一次,并且不能被重載
?7.靜態(tài)構(gòu)造函數(shù)不能有形式參數(shù)
?8.雖然靜態(tài)構(gòu)造和普通構(gòu)造函數(shù)名字一樣革娄,參數(shù)一樣倾贰,但是系統(tǒng)規(guī)定可以共存。
?9.靜態(tài)構(gòu)造函數(shù)是在普通構(gòu)造函數(shù)之前執(zhí)行稠腊,一般情況下用于初始化靜態(tài)字段躁染,或者攔截在實(shí)例化之前處理某些特定情況,可以在該函數(shù)中實(shí)現(xiàn).
用static 修飾的類是靜態(tài)類
靜態(tài)類的特點(diǎn)
1.靜態(tài)類不能被直接實(shí)例化
2.靜態(tài)類不能被繼承
3.靜態(tài)類中不能包含實(shí)例成員
4.靜態(tài)類就是特殊的密封類
5.靜態(tài)類中雖然不能包含實(shí)例成員,但是可以有常量
6.靜態(tài)類和實(shí)例調(diào)用方式不一樣架忌,普通類是以實(shí)例.的方式進(jìn)行調(diào)用吞彤,而靜態(tài)類是以類名.的方式進(jìn)行調(diào)用
7.靜態(tài)類的訪問速度相對(duì)而言要比實(shí)例類要快得多
8.如果類中包含靜態(tài)構(gòu)造函數(shù)和函數(shù)的入口main函數(shù),此時(shí)靜態(tài)構(gòu)造函數(shù)會(huì)在main函數(shù)之前調(diào)用
9.如果類中包含靜態(tài)構(gòu)造函數(shù)并不包含入口main函數(shù)叹放,此時(shí)靜態(tài)構(gòu)造函數(shù)會(huì)在main函數(shù)之后調(diào)用
字符串的常見API
1.獲取字符串長(zhǎng)度
public void Test1(){
//無論英文,特殊符號(hào)還是中文,長(zhǎng)度都是1個(gè)字節(jié).
string s = "你好中國(guó)xxx,";
Console.WriteLine("字符串長(zhǎng)度為:" + s.Length);}
2.字符串中查找字符串
public void Test2(){
//返回-1證明查不到
//如果能查到,返回的是該字符或者字符串在字符串中索引位置
string s = "HFDJasdgajSGDasjgdhagshjdg";
int i = s.IndexOf('F',3,1);
Console.WriteLine (i);}
3. 字符串提取,截取指定范圍內(nèi)的字符串
public void Test3(){
string s = "你好嗎,我的母親中國(guó)好棒!";
string temp = s.Substring(0,9);
Console.WriteLine (temp);}
字符串替換
public void Test4(){string s = "淫das,你dsa媽dsa個(gè)x,草曹操肏";//這些字符大多都是要被屏蔽的
string newStr = s.Replace("淫","*");Console.WriteLine (newStr);
//分幾種:1.親戚
//string pattern = @"[淫銀癮]|[草曹操肏]|[爹媽姑舅爺]";
//string newStr = Regex.Replace (s, pattern, "*");
//Console.WriteLine (newStr);}////// 字符串插入(指定位置)
///public void Test5(){string s = "你好,中國(guó)";
string newStr = s.Insert(0,"china");
Console.WriteLine (newStr);}
////// 判斷字符串以什么什么結(jié)尾
///public void Test6(){
string s = "你好,中國(guó)";bool b = s.EndsWith("中國(guó)");
if (b) {Console.WriteLine ("是以中國(guó)結(jié)束");}}
////// 字符串按照索引位置移除
///public void Test7(){string s = "你好,中國(guó)";string newStr = s.Remove(1,1);
Console.WriteLine (newStr);}
////// 字符串拼接
///public void Test8(){string s = "你好";s += "中國(guó)";Console.WriteLine (s);}
////// 判斷字符串是否相等
///public void Test9(){string s1 = "你好";string s2 = "你好1";
string s3 = "你好2";
//if (s1 == s2) {
//Console.WriteLine ("相等的字符串");
//}
if(string.Equals (s1, s2))
{Console.WriteLine ("字符串s1和s2相等");}}
////// 字符串轉(zhuǎn)換值類型
///public void Test10(){string s = "123.";//int i = int.Parse(s);int result = 0;bool l = int.TryParse (s, out result);
if (l) {Console.WriteLine ("轉(zhuǎn)換成功!~" + result);}?
else {throw new Exception ("傳入數(shù)據(jù)包含非法字符,請(qǐng)?zhí)幚?");}}//拋出一個(gè)異常