C#中的实例构造函数。

构造函数是类的一个特殊方法,在创建类的实例时将自动调用它。

C# 实例构造函数

C# 语言中有四种类型的实例构造函数。

  • 缺省构造函数(默认构造函数)
  • 参数化构造函数
  • 复制构造函数
  • 私有构造函数

C# 默认构造函数

没有参数的构造函数在C#中称为默认构造函数。每个类都有一个默认构造函数。当我们要初始化类成员的值时,使用默认构造函数。构造函数与类名同名。

C# 默认构造函数语法

public classname()

{

}

C# 默认构造函数示例

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace DefaultConstractor

{

    class Rahul

    {

        int x,y;

        public Rahul()   //default contructor

        {

            x = 10;

            y = 11;

        }

 

        public static void Main()

        {

            Rahul obj = new Rahul(); //an object is created , constructor is called

            Console.WriteLine(obj.x);

            Console.WriteLine(obj.y);

            Console.ReadLine();

        }

    }

}

C#参数化构造函数

具有至少一个参数的构造函数称为C#中的参数化构造函数。
当我们希望将类的每个实例初始化为不同的值时,使用参数化构造函数。

C#参数化构造函数语法

public classname(parameter x)

{

}

C#参数化构造函数示例

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Constructor

{

    class Rahul

    {

      public  int P, Q;

      public Rahul(int x, int y)  //decalaring Paremetrized Constructor with passing x,y parameter

        {

            P = x;

            Q = y;

        }

   }

    class MainClass

    {

        static void Main()

        {

            Rahul v = new Rahul(10, 11);   //Creating object of Parameterized Constructor and passing values  

            Console.WriteLine("P is =" + v.P );

            Console.WriteLine("Q is =" + v.Q);

            Console.ReadLine();

        }

    }

}

复制构造函数

如果我们创建一个新对象并希望从现有对象复制值,则可以使用Copy构造函数。
复制构造函数的目的是将新实例初始化为现有实例的值。

复制构造函数示例

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace ProgramCall

{

    class PEPSI

    {

        int A, B;

        public PEPSI(int X, int Y)

        {

            A = X;

            B = Y;

        }

        //复制构造函数

        public PEPSI(PEPSI P)

        {

            A = P.A;

            B = P.B;

        }

        public void Print()

        {

            Console.WriteLine("A  =  {0}\tB  =  {1}", A, B);

        }

    }

    class CopyConstructor

    {

        static void Main()

        {

            PEPSI P = new PEPSI(10,11);

            //调用复制构造函数

            PEPSI P1 = new PEPSI(P);

            P.Print();

            P1.Print();

            Console.Read();

        }

    }

}

私有构造函数

我们可以在C#中使用私有构造函数。私有构造函数是用私有说明符创建的。在C#语言中,我们不能创建一个至少包含一个私有构造函数的类的实例。它们通常用于只包含静态成员的类中。

私有构造函数示例

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace defaultConstractor

{

    public class like

    {

        private like()   //私有构造函数定义

        {

        }

        public static int currentview;

        public static int visitedCount()

        {

            return ++ currentview;

        }

    }

    class viewCountedetails

    {

        static void Main()

        {

            //like r = new like();   //错误

            Console.WriteLine("-------Private constructor----------");

            Console.WriteLine();

            like.currentview = 100;

            like.visitedCount();

            Console.WriteLine("Now the view count is: {0}", like.currentview);

            Console.ReadLine();

        }

    }

}
日期:2020-04-11 23:03:41 来源:oir作者:oir