0

作成したオブジェクト配列に 4 つの図形を配置するにはどうすればよいですか? 使用する

shapeArray[0] = (1,2,3,4)

私が考えることができるすべてであり、これは明らかに間違っています...

struct Shapes
{
    private int width;
    private int height;
    private int xAxis;
    private int yAxis;
}

Shapes[] shapeArray = new Shapes[4];
4

2 に答える 2

1

Shape に新しいコンストラクタを追加する必要があります。

struct Shape
{
    public Shape(int width, int height, int xAxis, int yAxis)
    {
        this.width = width;
        this.height = height;
        this.xAxis = xAxis;
        this.yAxis = yAxis;
    }

    private int width;
    private int height;
    private int xAxis;
    private int yAxis;

    public int Width { get { return width; } }
    public int Height { get { return height; } }
    public int XAxis { get { return xAxis; } }
    public int YAxis { get { return yAxis; } }
}

次に、それを使用して作成できます。

Shape[] shapes = new Shape[]{
    new Shape(1, 2, 3, 4),
    new Shape(2, 4, 6, 8),
    new Shape(1, 2, 3, 4),
    new Shape(4, 3, 2, 1)
};
于 2013-01-03T19:59:14.463 に答える
0

コンストラクターを作成していないためShapes、プロパティを明示的に設定する必要があります

shapeArray[0] = new Shapes;
shapeArray[0].width = 1; 
shapeArray[0].height = 2;
shapeArray[0].xAxis = 3;
shapeArray[0].yAxis = 4;

ただし、これを行う (そして変更可能な構造体を避ける) 適切な方法は、パブリック フィールドをプライベート化し、コンストラクターを構造体に追加することです。

public Shapes(int width, int height, int xAxis, int yAxis)
{ 
    this.width = width; 
    this.height = height;
    this.xAxis = xAxis;
    this.yAxis = yAxis;
}

それからあなたはただ使うでしょう

shapeArray[0] = new Shapes(1, 2, 3, 4);
于 2013-01-03T19:57:59.290 に答える