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

C#構(gòu)造函數(shù)詳解

 更新時(shí)間:2022年04月18日 13:35:28   作者:農(nóng)碼一生  
本文詳細(xì)講解了C#中的構(gòu)造函數(shù),文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

一、簡(jiǎn)介

構(gòu)造函數(shù),基本用法是在類(lèi)對(duì)象聲明的時(shí)候完成初始化工作。

二、實(shí)例構(gòu)造函數(shù)

1、構(gòu)造函數(shù)的名字與類(lèi)名相同。

2、使用 new 表達(dá)式創(chuàng)建類(lèi)的對(duì)象或者結(jié)構(gòu)(例如int)時(shí),會(huì)調(diào)用其構(gòu)造函數(shù)。并且通常初始化新對(duì)象的數(shù)據(jù)成員。

3、除非類(lèi)是靜態(tài)的,否則會(huì)為沒(méi)有構(gòu)造函數(shù)的類(lèi),自動(dòng)生成一個(gè)默認(rèn)構(gòu)造函數(shù),并使用默認(rèn)值來(lái)初始化對(duì)象字段。

4、構(gòu)造函數(shù)可以有參數(shù),可以以多態(tài)的形式存在多個(gè)構(gòu)造函數(shù)。

代碼:

class CoOrds
    {
        public int x, y;
        // 實(shí)例構(gòu)造函數(shù)(默認(rèn)構(gòu)造函數(shù))
        public CoOrds()
        {
            x = 0;
            y = 0;
        }
        // 具有兩個(gè)參數(shù)的構(gòu)造函數(shù)
        public CoOrds(int x, int y)
        {
            this.x = x;
            this.y = y;
        }

        // 重寫(xiě)toString方法
        public override string ToString()
        {
            return (String.Format("({0},{1})", x, y));
        }
        static void Main(string[] args)
        {
            CoOrds p1 = new CoOrds();//調(diào)用默認(rèn)的無(wú)參構(gòu)造函數(shù)
            CoOrds p2 = new CoOrds(5, 3);//調(diào)用兩個(gè)參數(shù)構(gòu)造函數(shù)

            // 使用重寫(xiě)ToString方法顯示結(jié)果
            Console.WriteLine("CoOrds #1 at {0}", p1);
            Console.WriteLine("CoOrds #2 at {0}", p2);
            Console.ReadKey();
        }
    }

    /* Output:
     CoOrds #1 at (0,0)
     CoOrds #2 at (5,3)        
    */

分析:

1.其中CoOrds()是構(gòu)造函數(shù),諸如此類(lèi)不帶參數(shù)的構(gòu)造函數(shù)稱(chēng)為“默認(rèn)構(gòu)造函數(shù)”。

2.CoOrds(int x, int y)同樣也是構(gòu)造函數(shù),構(gòu)造函數(shù)可以有參數(shù),允許多態(tài)。

三、靜態(tài)構(gòu)造函數(shù)

1.靜態(tài)構(gòu)造函數(shù)不使用訪(fǎng)問(wèn)修飾符或不具有參數(shù)。
2.在創(chuàng)建第一個(gè)實(shí)例或引用任何靜態(tài)成員之前,將自動(dòng)調(diào)用靜態(tài)構(gòu)造函數(shù)以初始化類(lèi)。
3.不能直接調(diào)用靜態(tài)構(gòu)造函數(shù)。
4.用戶(hù)無(wú)法控制在程序中執(zhí)行靜態(tài)構(gòu)造函數(shù)的時(shí)間。
5.靜態(tài)構(gòu)造函數(shù)的一種典型用法是在類(lèi)使用日志文件且將構(gòu)造函數(shù)用于將條目寫(xiě)入到此文件中時(shí)使用。
6.靜態(tài)構(gòu)造函數(shù)對(duì)于創(chuàng)建非托管代碼的包裝類(lèi)也非常有用,這種情況下構(gòu)造函數(shù)可調(diào)用 LoadLibrary 方法。
7.如果靜態(tài)構(gòu)造函數(shù)引發(fā)異常,運(yùn)行時(shí)將不會(huì)再次調(diào)用該函數(shù),并且類(lèi)型在程序運(yùn)行所在的應(yīng)用程序域的生存期內(nèi)將保持未初始化。

代碼1:

class TestClass
    {
        public static int x = 0;
        //構(gòu)造函數(shù)
        TestClass()
        {
            x = 1;
        }
        //靜態(tài)構(gòu)造函數(shù)
        static TestClass()
        {    //第二步,執(zhí)行x = 2
            x = 2;
        }    
        //第一步,程序入口Main最先執(zhí)行。然后執(zhí)行public static int x = 0 接著執(zhí)行靜態(tài)構(gòu)造函數(shù)。
        public static void Main(string[] args)
        {
            Console.WriteLine("x:{0}", x); //打印,x = 2
            TestClass Test = new TestClass();//第三步執(zhí)行構(gòu)造函數(shù),此時(shí)x = 1
            Console.WriteLine("x:{0}", x); //打印 x = 1
            Console.Read();
        }
    }

分析1:

1.Main是程序入口,當(dāng)執(zhí)行Main的時(shí)候,最先執(zhí)行public static int x = 0
2.接著執(zhí)行靜態(tài)構(gòu)造函數(shù),此時(shí) x = 2
3.然后執(zhí)行Main函數(shù)里面的內(nèi)容,打印 x,此時(shí) x = 2
4.初始化TestClass,然后會(huì)執(zhí)行構(gòu)造函數(shù),此時(shí) x = 1
5.打印 x = 1

代碼2:

public class A
    {
        public static readonly int x;
        static A()
        {
            //第二步,調(diào)用B.y,此處B.y = 0,因?yàn)閕nt類(lèi)型在初始化階段,會(huì)給賦默認(rèn)值,默認(rèn)值為0。最后x = 0 + 1(返回給第一步)
            x = B.y + 1;
        }
    }
    public class B
    {
        //第一步,調(diào)用A.x,然后執(zhí)行類(lèi)A的靜態(tài)構(gòu)造函數(shù),等待返回(第二步返回的A.x = 1,所以y = 1 + 1)
        public static int y = A.x + 1;
        public static void Main(string[] args)
        {
            //第三步,A.x = 1,y = 2。
            Console.WriteLine("x:{0},y:{1}。", A.x, y);
            Console.ReadLine();
        }
    }

分析2:

1.首先,每一個(gè)項(xiàng)目有且只能有一個(gè)靜態(tài)類(lèi)的Main函數(shù)作為入口函數(shù)。而入口函數(shù)是最先執(zhí)行的。
2.由于Main函數(shù)在B類(lèi)里面,首先會(huì)初始化B類(lèi)。而類(lèi)的初始化順序是:類(lèi)里的靜態(tài)變量,然后執(zhí)行靜態(tài)構(gòu)造函數(shù)。
3.運(yùn)行起先執(zhí)行 public static int y = A.x + 1 這個(gè),執(zhí)行的時(shí)候,會(huì)先把 y 初始化為0,然后計(jì)算 y 的值。
4.計(jì)算 y 的值的時(shí)候,調(diào)用了 A 的靜態(tài)變量 x 。所以會(huì)先初始化A。
5.初始化A時(shí)首先去執(zhí)行 public static readonly int x ,先把 x 初始化為0。
6.然后執(zhí)行A的靜態(tài)構(gòu)造函數(shù) x = B.y + 1 此時(shí) y 已經(jīng)初始化為0了。
7.計(jì)算得到 x = 1。然后回到 public static int y = A.x + 1 得到 y = 2。
8.然后再執(zhí)行Main函數(shù)的內(nèi)容。得出結(jié)果x=1,y=2

四、私有構(gòu)造函數(shù)

私有構(gòu)造函數(shù)是一種特殊的實(shí)例構(gòu)造函數(shù)。 它通常用于只包含靜態(tài)成員的類(lèi)中。 如果類(lèi)具有一個(gè)或多個(gè)私有構(gòu)造函數(shù)而沒(méi)有公共構(gòu)造函數(shù),則其他類(lèi)(除嵌套類(lèi)外)無(wú)法創(chuàng)建該類(lèi)的實(shí)例。

代碼:

public class PrivateConstructor
    {
        private PrivateConstructor()
        {
            //PrivateTest a = new PrivateTest();
            //注釋打開(kāi)會(huì)報(bào)錯(cuò),錯(cuò)誤信息:不可訪(fǎng)問(wèn),因?yàn)樗鼙Wo(hù)級(jí)別限制。因?yàn)樗接袠?gòu)造函數(shù)無(wú)法在類(lèi)的外面實(shí)例化。
        }
        public class PrivateTest
        {
            int i;
            private PrivateTest()
            {
                i = 3;
            }
            static void Main(string[] args)
            {
                PrivateConstructor t = new PrivateConstructor(); //嵌套類(lèi)允許實(shí)例化。
                PrivateTest p = new PrivateTest(); //類(lèi)的內(nèi)部允許實(shí)例化。
                Console.WriteLine("i:{0}", p.i); //結(jié)果:i:3
                Console.Read();
            }
        }
    }

分析:

聲明空構(gòu)造函數(shù)可阻止自動(dòng)生成默認(rèn)構(gòu)造函數(shù)。 請(qǐng)注意,如果不對(duì)構(gòu)造函數(shù)使用訪(fǎng)問(wèn)修飾符,則在默認(rèn)情況下它仍為私有構(gòu)造函數(shù)。 但是,通常會(huì)顯式地使用 private 修飾符來(lái)清楚地表明該類(lèi)不能被實(shí)例化。

到此這篇關(guān)于C#構(gòu)造函數(shù)的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論