实例构造函数(C# 编程指南)

使用 new 表达式创建某个的对象时,会使用实例构造函数创建和初始化所有实例成员变量。 要初始化静态类或非静态类中的静态变量,必须定义静态构造函数。 有关更多信息,请参见 静态构造函数(C# 编程指南)

下面的示例演示实例构造函数:

class CoOrds
{
    public int x, y;

    // constructor
    public CoOrds()
    {
        x = 0;
        y = 0;
    }
}

备注

为了清楚起见,此类包含公共字段。建议在编程时不要使用公共字段,因为这种做法会使程序中任何位置的任何方法都可以不受限制、不经验证地访问对象的内部组件。数据成员通常应当为私有的,并且只应当通过类方法和属性来访问。

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

// tcA 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;
    }

    // tcA 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();
    }
}
/* Output:
 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();
    }
}
// Output:  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();
    }
}
/* Output:
    Area of the circle = 19.63
    Area of the cylinder = 86.39
*/

有关调用基类构造函数的更多示例,请参见 virtual(C# 参考)override(C# 参考)base(C# 参考)

请参见

参考

类和结构(C# 编程指南)

构造函数(C# 编程指南)

析构函数(C# 编程指南)

static(C# 参考)

概念

C# 编程指南