好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

详解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...

}

结构类型的构造函数与类的构造函数类似,但是 structs 不能包含显式默认构造函数,因为编译器将自动提供一个构造函数。此构造函数会将 struct 中的每个字段初始化为默认值。然而,只有当 struct 用 new 实例化时,才会调用此默认构造函数。例如,下面的代码使用 Int32 的默认构造函数,因此您可以确信整数已初始化:

?

int i = new int ();

Console.WriteLine(i);

不过,下面的代码却会导致编译器错误,因为它没有使用 new,而且尝试使用尚未初始化的对象:

?

int i;

Console.WriteLine(i);

或者,基于 structs 的对象(包括所有内置数值类型)可以初始化或赋值后使用,如下面的示例所示:

?

int a = 44; // Initialize the value type...

int b;

b = 33;   // Or assign it before using it.

Console.WriteLine( "{0}, {1}" , a, b);

因此对值类型调用默认构造函数不是必需的。
类和 structs 都可以定义具有参数的构造函数。带参数的构造函数必须通过 new 语句或 base 语句来调用。类和 structs 还可以定义多个构造函数,并且二者均不需要定义默认构造函数。例如:

?

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(C# 参考)。
在派生类中,如果不使用 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 或 protectedinternal。这些访问修饰符定义类的用户构造该类的方式。有关更多信息,请参见访问修饰符。


实例构造函数
使用 new 表达式创建某个类的对象时,会使用实例构造函数创建和初始化所有实例成员变量。要初始化静态类或非静态类中的静态变量,必须定义静态构造函数。
下面的示例演示实例构造函数:

?

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。有关默认值的更多信息,请参见 默认值表(C# 参考)。因此,由于 CoOrds 类的默认构造函数将所有数据成员都初始化为零,因此可以将它完全移除,而不会更改类的工作方式。本主题的稍后部分的示例 1 中提供了使用多个构造函数的完整示例,示例 2 中提供了自动生成的构造函数的示例。
也可以用实例构造函数来调用基类的实例构造函数。类构造函数可通过初始值设定项来调用基类的构造函数,如下所示:

?

class Circle : Shape

{

   public Circle( double radius)

     : base (radius, 0)

   {

   }

}

在此示例中,Circle 类将表示半径和高度的值传递给 Shape(Circle 从它派生而来)提供的构造函数。使用 Shape 和 Circle 的完整示例请见本主题中的示例 3。
示例 1
下面的示例说明包含两个类构造函数的类:一个类构造函数没有参数,另一个类构造函数带有两个参数。

?

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();

   }

}

输出:

?

CoOrds #1 at (0,0)

CoOrds #2 at (5,3)   

示例 2
在此示例中,类 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();

   }

}

输出:

?

Name: , Age: 0

注意,age 的默认值为 0,name 的默认值为 null。有关默认值的更多信息,请参见 默认值表(C# 参考)。
示例 3
下面的示例说明使用基类初始值设定项。 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();

   }

}

输出:

?

Area of the circle = 19.63

Area of the cylinder = 86.39

 

dy("nrwz");

查看更多关于详解C#编程中构造函数的使用的详细内容...

  阅读:71次