初始化包含其他对象数组的对象数组?

时间:2012-11-22 20:36:25

标签: c# arrays class

我正在尝试声明一系列建筑物,但与每栋建筑相关的楼层从未初始化。我想为每个楼层实例的每个楼层实例的数据成员提供值:

class Floor
{
    public int number;
    public int rooms;   
    //constructor
    Floor()
    {
        rooms = 5; 
        number= 0;           
    }
}

class Building
{
    public Floor[] floors= new Floor[6];
}

public partial class frmF2 : Form
{
    Building[] x = new Building[7];
    ...
}        

1 个答案:

答案 0 :(得分:2)

但它非常难看。

class Building
{
    Building()
    {
      floors = new Floor[6];
      for(int i=0; i<6;++i)
          floors[i] = new Floor();
    }

    public Floor[] floors;
}