0

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

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];
    ...
}        
4

1 回答 1

2

但它非常难看。

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

    public Floor[] floors;
}
于 2012-11-22T20:42:55.860 回答