天天看点

C#类成员-构造函数

       任何时候,只要创建类的实例,就会调用它的构造函数。类可能有多个接受不同参数的构造函数。构造函数使得程序员可设置默认值、限制实例化以及编写灵活且便于阅读的代码。

如果您没有为类提供构造函数,在创建对象时则由 C# 默认创建一个构造函数,该构造函数实例化对象,并将成员变量设置为默认值表(C# 参考)中列出的默认值。静态类和结构也可以有构造函数。

一、使用构造函数

构造函数是在创建类的对象时调用的类方法。构造函数名与类名称相同,同城初始化新对象的数据成员。

在下面的示例中,使用一个简单的构造函数定义了名为 Taxi 的类。然后使用 new 运算符来实例化该类。在为新对象分配内存之后,new 运算符立即调用 Taxi 构造函数。

public class Taxi

{

    public bool isInitialized;

    public Taxi()

    {

        isInitialized = true;

    }

}

class TestTaxi

{

    static void Main()

    {

        Taxi t = new Taxi();

        Console.WriteLine(t.isInitialized);

    }

}

不带参数的构造函数称为“默认构造函数”。无论何时,只要使用 new 运算符实例化对象,并且不为 new 提供任何参数,就会调用默认构造函数。

除非类是 static 的,否则 C# 编译器将为无构造函数的类提供一个公共的默认构造函数,以便该类可以实例化。

通过将构造函数设置为私有构造函数,可以阻止类被实例化,如下所示:

class NLog

{

    // Private Constructor:

    private NLog() { }

    public static double e = Math.E;  //2.71828...

}

类可以定义具有参数的构造函数。带参数的构造函数必须通过 new 语句或 base 语句来调用。类 还可以定义多个构造函数,并且不需要定义默认构造函数。例如:

public class Employee

{

    public int salary;

    public Employee(int annualSalary)

    {

        salary = annualSalary;

    }

    public Employee(int weeklySalary, int numberOfWeeks)

    {

        salary = weeklySalary * numberOfWeeks;

    }

}

可以使用下列语句中的任一个语句来创建此类:

Employee e1 = new Employee(30000);

Employee e2 = new Employee(500, 52);

构造函数可以使用 base 关键字来调用基类的构造函数。例如:

public class Manager : Employee

{

    public Manager(int annualSalary)

        : base(annualSalary)

    {

        //Add further instructions here.

    }

}

在此示例中,基类的构造函数在执行构造函数块之前被调用。base 关键字可带参数使用,也可不带参数使用。构造函数的任何参数都可用作 base 的参数,或用作表达式的一部分。

在派生类中,如果不使用 base 关键字来显式调用基类构造函数,则将隐式调用默认构造函数(如果有的话)。这意味着下面的构造函数声明在效果上是相同的:

public Manager(int initialdata)

{

    //Add further instructions here.

}

public Manager(int initialdata)

    : base()

{

    //Add further instructions here.

}

如果基类没有提供默认构造函数,派生类必须使用 base 显式调用基构造函数。

构造函数可以使用 this 关键字调用同一对象中的另一构造函数。和 base 一样,this 可带参数使用也可不带参数使用,构造函数中的任何参数都可用作 this 的参数,或者用作表达式的一部分。例如,可以使用 this 重写前一示例中的第二个构造函数:

public Employee(int weeklySalary, int numberOfWeeks)

    : this(weeklySalary * numberOfWeeks)

{

}

上一示例中对 this 关键字的使用导致此构造函数被调用:

public Employee(int annualSalary)

{

    salary = annualSalary;

}

构造函数可以标记为 public、private、protected、internal 或 protected internal。这些访问修饰符定义类的用户构造该类的方式。

使用 static 关键字可以将构造函数声明为静态构造函数。在访问任何静态字段之前,都将自动调用静态构造函数,它们通常用于初始化静态类成员。

二、实例构造函数

实例构造函数用于创建和初始化实例。创建新对象时将调用类构造函数,例如:

class CoOrds

{

    public int x, y;

    // constructor

    public CoOrds()

    {

        x = 0;

        y = 0;

    }

}

无论何时创建基于 CoOrds 类的对象,都会调用此构造函数。诸如此类不带参数的构造函数称为“默认构造函数”。然而,提供其他构造函数通常十分有用。例如,可以向 CoOrds 类添加构造函数,以便可以为数据成员指定初始值:

// A constructor with two arguments:

public CoOrds(int x, int y)

{

    this.x = x;

    this.y = y;

}

这样便可以用默认或特定的初始值创建 CoOrd 对象,如下所示:

CoOrds p1 = new CoOrds();

CoOrds p2 = new CoOrds(5, 3);

如果类没有默认构造函数,将自动生成一个构造函数,并且将用默认值来初始化对象字段,例如,int 被初始化为 0。

下面的示例说明包含两个类构造函数的类:一个类构造函数没有参数,另一个类构造函数带有两个参数。

class CoOrds

{

    public int x, y;

    // Default constructor:

    public CoOrds()

    {

        x = 0;

        y = 0;

    }

    // A constructor with two arguments:

    public CoOrds(int x, int y)

    {

        this.x = x;

        this.y = y;

    }

    // Override the ToString method:

    public override string ToString()

    {

        return (String.Format("({0},{1})", x, y));

    }

}

class MainClass

{

    static void Main()

    {

        CoOrds p1 = new CoOrds();

        CoOrds p2 = new CoOrds(5, 3);

        // Display the results using the overriden ToString method:

        Console.WriteLine("CoOrds #1 at {0}", p1);

        Console.WriteLine("CoOrds #2 at {0}", p2);

        Console.ReadKey();

    }

}

在此示例中,类 Person 没有任何构造函数;在这种情况下,将自动提供默认构造函数,同时将字段初始化为它们的默认值。

public class Person

{

    public int age;

    public string name;

}

class TestPerson

{

    static void Main()

    {

        Person person = new Person();

        Console.WriteLine("Name: {0}, Age: {1}", person.name, person.age);

        // Keep the console window open in debug mode.

        Console.WriteLine("Press any key to exit.");

        Console.ReadKey();

    }

}

// Output:  Name: , Age: 0

注意,age 的默认值为 0,name 的默认值为 null。

下面的示例说明使用基类初始值设定项。Circle 类是从通用类 Shape 派生的,Cylinder 类是从 Circle 类派生的。每个派生类的构造函数都使用其基类的初始值设定项。

abstract class Shape

{

    public const double pi = Math.PI;

    protected double x, y;

    public Shape(double x, double y)

    {

        this.x = x;

        this.y = y;

    }

    public abstract double Area();

}

class Circle : Shape

{

    public Circle(double radius)

        : base(radius, 0)

    {

    }

    public override double Area()

    {

        return pi * x * x;

    }

}

class Cylinder : Circle

{

    public Cylinder(double radius, double height)

        : base(radius)

    {

        y = height;

    }

    public override double Area()

    {

        return (2 * base.Area()) + (2 * pi * x * y);

    }

}

class TestShapes

{

    static void Main()

    {

        double radius = 2.5;

        double height = 3.0;

        Circle ring = new Circle(radius);

        Cylinder tube = new Cylinder(radius, height);

        Console.WriteLine("Area of the circle = {0:F2}", ring.Area());

        Console.WriteLine("Area of the cylinder = {0:F2}", tube.Area());

        // Keep the console window open in debug mode.

        Console.WriteLine("Press any key to exit.");

        Console.ReadKey();

    }

}

三、私有构造函数

私有构造函数是一种特殊的实例构造函数。它通常用在只包含静态成员的类中。如果类具有一个或多个私有构造函数而没有公共构造函数,则其他类(除嵌套类外)无法创建该类的实例。

class NLog

{

    // Private Constructor:

    private NLog() { }

    public static double e = Math.E;  //2.71828...

}

声明空构造函数可阻止自动生成默认构造函数。注意,如果您不对构造函数使用访问修饰符,则在默认情况下它仍为私有构造函数。但是,通常显式地使用 private 修饰符来清楚地表明该类不能被实例化。

当没有实例字段或实例方法(如 Math 类)时或者当调用方法以获得类的实例时,私有构造函数可用于阻止创建类的实例。如果类中的所有方法都是静态的,可考虑使整个类成为静态的。

下面是使用私有构造函数的类的示例

public class Counter

{

    private Counter() { }

    public static int currentCount;

    public static int IncrementCount()

    {

        return ++currentCount;

    }

}

class TestCounter

{

    static void Main()

    {

        // If you uncomment the following statement, it will generate

        // an error because the constructor is inaccessible:

        // Counter aCounter = new Counter();   // Error

        Counter.currentCount = 100;

        Counter.IncrementCount();

        Console.WriteLine("New count: {0}", Counter.currentCount);

        // Keep the console window open in debug mode.

        Console.WriteLine("Press any key to exit.");

        Console.ReadKey();

    }

}

// Output: New count: 101

注意,如果您取消注释该示例中的以下语句,它将生成一个错误,因为该构造函数受其保护级别的限制而不可访问:

// Counter aCounter = new Counter();   // Error

四、静态构造函数

静态构造函数用于初始化任何静态数据,或用于执行仅需执行一次的特定操作。在创建第一个实例或引用任何静态成员之前,将自动调用静态构造函数。

class SimpleClass

{

    // Static variable that must be initialized at run time.

    static readonly long baseline;

    // Static constructor is called at most one time, before any

    // instance constructor is invoked or member is accessed.

    static SimpleClass()

    {

        baseline = DateTime.Now.Ticks;

    }

}

静态构造函数具有以下特点:

·静态构造函数既没有访问修饰符,也没有参数。

·在创建第一个实例或引用任何静态成员之前,将自动调用静态构造函数来初始化类。

·无法直接调用静态构造函数。

·在程序中,用户无法控制何时执行静态构造函数。

·静态构造函数的典型用途是:当类使用日志文件时,将使用这种构造函数向日志文件中写入项。

·静态构造函数在为非托管代码创建包装类时也很有用,此时该构造函数可以调用 LoadLibrary 方法。

·如果静态构造函数引发异常,运行时将不会再次调用该构造函数,并且在程序运行所在的应用程序域的生存期内,类型将保持未初始化。

在此示例中,类 Bus 有一个静态构造函数和一个静态成员 Drive()。当调用 Drive() 时,将调用静态构造函数来初始化类。

public class Bus

 {

     // static variable used by all Bus instances

     // Represents the time the first bus of the day starts its route.

     protected static readonly DateTime globalStartTime;

     // Instance readonly variable

     protected int RouteNumber { get; set; }

     // Static constructor to initialize static variable.

     // It is invoked before the first instance constructor is called.

     static Bus()

     {

         globalStartTime = DateTime.Now;

         Console.WriteLine("Static ctor sets global start time to {0}", globalStartTime.ToLongTimeString());

     }

     // Instance constructor

     public Bus(int routeNum)

     {

         RouteNumber = routeNum;

         Console.WriteLine("{0} is created.", RouteNumber);

     }

     // Instance method.

     public void Drive()

     {

         TimeSpan elapsedTime = DateTime.Now - globalStartTime;

         // For demonstration purposes we treat milliseconds as minutes to

         // simulate actual bus times. Do not do this in your actual bus schedule program!

         Console.WriteLine("{0} is starting its route {1:N2} minutes after global start time {2}.",

                                 this.RouteNumber,

                                 elapsedTime.TotalMilliseconds,

                                 globalStartTime.ToShortTimeString());

     }

 }

 class TestBus

 {

     static void Main()

     {

         Bus bus = new Bus(71);

         bus.Drive();

         // Wait for next bus to warm up.

         System.Threading.Thread.Sleep(25);

         Bus bus2 = new Bus(72);

         bus2.Drive();

         // Keep the console window open in debug mode.

         System.Console.WriteLine("Press any key to exit.");

         System.Console.ReadKey();

     }

 }

五、如何编写复制构造函数

与有些语言不同,C# 不提供复制构造函数。如果您创建了新的对象并希望从现有对象复制值,您必须自行编写适当的方法。

在本示例中,Person 类包含一个构造函数,该构造函数接受另一个 Person 类型的对象作为参数。然后此对象的字段中的内容将分配给新对象中的字段。

class Person

{

    private string name;

    private int age;

    // Copy constructor.

    public Person(Person previousPerson)

    {

        name = previousPerson.name;

        age = previousPerson.age;

    }

    // Instance constructor.

    public Person(string name, int age)

    {

        this.name = name;

        this.age = age;

    }

    // Get accessor.

    public string Details

    {

        get

        {

            return name + " is " + age.ToString();

        }

    }

}

class TestPerson

{

    static void Main()

    {

        // Create a new person object.

        Person person1 = new Person("George", 40);

        // Create another new object, copying person.

        Person person2 = new Person(person1);

        Console.WriteLine(person2.Details);

        // Keep the console window open in debug mode.

        Console.WriteLine("Press any key to exit.");

        Console.ReadKey();

    }

}

// Output: George is 40

六、构造函数及参数执行顺序说明

三个类分开来写的,一个父类,一个子类,一个测试类:

父类:

using system;

namespace testinherit

{

/// <summary>

/// father 的摘要说明。

/// </summary>

      public class father

      {

           public int father_a=111;

           public int father_aa=1111;

           public readonly int father_c=7777;

           public static int father_e=222;

           public static int father_ee;

           static father()

           {

                 father_e=5555;

                 father_ee=3333;

           }

           public father()

           {

                 father_ee=4444;

           }

           public father(int a)

           {

                 father_a=a;

           }

      }

}

子类:

using system;

namespace testinherit

{

      /// <summary>

      /// sun 的摘要说明。

      /// </summary>

      public class sun:father

      {

           public int sun_int=9999;

           public static int sun_b=111;

           public static int sun_c;

           public sun()

           {

                 sun_c=222;

           }

           static sun()

           {

                 sun_c=333;

           }

           public sun(int a)

           {

                 sun_int=a;

           }

      }

}

测试class1类

using system;

namespace testinherit

{

      /// <summary>

      /// class1 的摘要说明。

      /// </summary>

      class class1

      {

           /// <summary>

           /// 应用程序的主入口点。

           /// </summary>

           [stathread]

           static void main(string[] args)

           {

                 //

                 // todo: 在此处添加代码以启动应用程序

                 //

                 sun sun1=new sun ();

           }

      }

}

然后一路f11,进行跟踪,可以得到完整的执行顺序如下所示:

1.子类静态变量;

2.子类静态构造函数;

3.子类非静态变量;

4.父类静态变量;

5.父类静态构造函数;

6.父类非静态变量;

7.父类无参构造函数;

8.子类无参构造函数;

C#类成员-构造函数

如果main()方法中改为sun sun1=new sun (111),顺序为;

1.子类静态变量;

2.子类静态构造函数;

3.子类非静态变量;

4.父类静态变量;

5.父类静态构造函数;

6.父类非静态变量;

7.父类无参构造函数;

8.子类有参构造函数;

C#类成员-构造函数

如果子类的有参构造函数改为:

public sun(int a):base(a)

{

      sun_int=a;

}

则顺序又有所改变:

1.子类静态变量;

2.子类静态构造函数;

3.子类非静态变量;

4.父类静态变量;

5.父类静态构造函数;

6.父类非静态变量;

7.父类有参构造函数;

8.子类有参构造函数;

C#类成员-构造函数

以上测试改为父类声明,子类实现,结果也是一样,即mian()方法中改为father sun1=new sun ();

最后作一个总结:

1.一般原理是被依赖的先构造,依赖于人的后构造,c#中是同层依赖(成员变量,其他的类变量等)优先于跨层依赖(父子关系)构造(注意java中正好相反); (子类的变量是不是可以理解为同层的依赖呢??)

2.静态构造函数,静态参数都是优先于非静态构造函数,非静态参数构造或初始化。