欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

.NET中的靜態(tài)與非靜態(tài)的區(qū)別分析

 更新時(shí)間:2013年03月27日 08:58:26   作者:  
.NET中的靜態(tài)與非靜態(tài)的區(qū)別分析,需要的朋友可以參考一下

靜態(tài)類 vs 普通類

靜態(tài)類與普通類的區(qū)別有以下幾點(diǎn):

1)靜態(tài)類無法實(shí)例化而普通類可以;

2)靜態(tài)類只能從System.Object基類繼承;普通可以繼承其它任何非static和非sealed類,但也只能繼承一個(gè)類;

3)靜態(tài)類不能繼承接口;普通類可以繼承多個(gè)接口;

4)靜態(tài)類只能包含靜態(tài)成員;普通類可以包含靜態(tài)和非靜態(tài)成員;

5)靜態(tài)類不能作為字段,方法參數(shù)和局部變量使用;普通類可以;

靜態(tài)類的意義:

可以用靜態(tài)類封裝一組不與任何對象相關(guān)聯(lián)的方法,如Math類,Console類。

 

靜態(tài)構(gòu)造函數(shù) vs 實(shí)例構(gòu)造函數(shù)

靜態(tài)構(gòu)造函數(shù)與實(shí)例構(gòu)造函數(shù)的區(qū)別有以下幾點(diǎn):

1)靜態(tài)構(gòu)造函數(shù)屬于類,在第一次用到該類時(shí)執(zhí)行且只執(zhí)行一次;實(shí)例構(gòu)造函數(shù)屬于對象,在每次初始化一個(gè)新對象的時(shí)候都會執(zhí)行;

2)靜態(tài)構(gòu)造函數(shù)只能定義一次,且不能包含參數(shù);實(shí)例構(gòu)造函數(shù)可以定義重載,且可以包含參數(shù);

3)靜態(tài)構(gòu)造函數(shù)只能訪問類型的靜態(tài)字段;實(shí)例構(gòu)造函數(shù)可以訪問類型的靜態(tài)和非靜態(tài)字段;

4)靜態(tài)類不能包含訪問修飾符,默認(rèn)為private.

靜態(tài)構(gòu)造函數(shù)的意義:

設(shè)置類型的初始化,例如初始化類型需要的實(shí)例對象,為類型的靜態(tài)字段賦值等。

 

靜態(tài)方法 vs 實(shí)例方法

靜態(tài)方法與實(shí)例方法的區(qū)別有以下幾點(diǎn):

1)靜態(tài)方法屬于類,通過類來調(diào)用;實(shí)例方法屬于對象,通過對象來調(diào)用;

2)靜態(tài)方法不能訪問類的非靜態(tài)成員;

靜態(tài)方法的意義:

完成一個(gè)與特定對象無關(guān)的功能。

 

靜態(tài)字段 vs 非靜態(tài)字段

靜態(tài)字段與非靜態(tài)字段的區(qū)別:

靜態(tài)字段屬于類,通過類來調(diào)用;非靜態(tài)字段屬于對象,通過對象來調(diào)用。

靜態(tài)字段的意義:

可以用靜態(tài)字段來記錄一些屬于類本身的信息。

 

代碼演示


復(fù)制代碼 代碼如下:

public class Test
     {
         public int i = 10;
         public static int j = 20;
         public int k;

         public Test()
         {
             Console.WriteLine("i is a non-static field, its value is {0}", i);
             Console.WriteLine("j is a static field, its value is {0}", j);
         }

         public Test(int k)
         {
             this.k = k;
             Console.WriteLine("i is a non-static field, its value is {0}", i);
             Console.WriteLine("j is a static field, its value is {0}", j);
             Console.WriteLine("k is a non-static field, its value is {0}", k);
         }

         static Test()
         {
             Console.WriteLine("I am a static constructor, I couldn't contain any parameters!");
             Console.WriteLine("I couldn't access to the non-static field i, I can only access to the static field j, the value of j is {0}", j);
         }

         public void Print()
         {
             Console.WriteLine("I am a instance method, I can access both the non-static field and the static field!");
             Console.WriteLine("The value of i is {0} and the value of j is {1}", i, j);
         }

         public static void StaticPrint()
         {
             Console.WriteLine("I am a static method, I couldnt access to the non-static field i,  I can only access to the static field j, the value of j is {0}", j);
         }
     }

運(yùn)行結(jié)果


從運(yùn)行結(jié)果可以看出,在Main方法中創(chuàng)建了兩個(gè)Test對象,靜態(tài)構(gòu)造函數(shù)只執(zhí)行了一次,且先于實(shí)例構(gòu)造函數(shù)執(zhí)行,實(shí)例構(gòu)造函數(shù)在兩次實(shí)例化過程中各執(zhí)行了一次。同時(shí)可以看到我們是通過Test.j來調(diào)用的j字段,而i字段和k字段的調(diào)用則通過Test的兩個(gè)對象t1和t2調(diào)用。同樣的,靜態(tài)方法StaticPrint也是用Test類來調(diào)用,而實(shí)例方法Print通過Test類的對象來調(diào)用。

Fighting like Allen Iverson! Never never give up!

相關(guān)文章

最新評論