【问题标题】:Subtract array values of Object减去 Object 的数组值
【发布时间】:2014-03-25 06:17:37
【问题描述】:

如何从 Cell[5,4] 中减去 Cell[3,5]?我有一个单元对象,它创建一个二维数组并在其中设置战士对象。 我试过了,但是没有用

Cell[,] cells = new Cell[6,6];
cells[3, 5] = new Cell(warrior);
cells[5,4] = new Cell(warrior);

...

int x1 = cells[3, 5].x - cells[5, 4].x;
int x2 = cells[3, 5].y - cells[5, 4].y;
Console.WriteLine(x1);
Console.WriteLine(x2);

我的 Cell 类是这样的:

public class Cell
{
    public int _x;
    public int _y;
    public Warrior _warrior; 
}

【问题讨论】:

  • 你是什么意思,I tried this but id didnot work?你能解释一下吗?
  • 你有Cell(Warrior warrior)?类型的构造函数吗?
  • 公共单元格(战士 getWarrior){ this._warrior = getWarrior; } 我是这样设置战士的

标签: c# arrays multidimensional-array


【解决方案1】:

我已经写了一个示例代码试试这样..

 Warrior warrior = new Warrior(25,24);
        Warrior warrior1 = new Warrior(20,20);

        Cell[,] cells = new Cell[6, 6];
        cells[3, 5] = new Cell(warrior);
        cells[5, 4] = new Cell(warrior1);

        int x1 = cells[3, 5]._x - cells[5, 4]._x;
        int x2 = cells[3, 5]._y - cells[5, 4]._y;
        Console.WriteLine(x1);
        Console.WriteLine(x2);

 public class Cell
{
    public Cell(Warrior warrior)
    {
        _x = warrior.x;
        _y = warrior.y;
    }

    public int _x;
    public int _y;
    public Warrior _warrior;
}

public class Warrior
{
    public Warrior(int x, int y)
    {
        this.x = x;
        this.y = y;
    }

    public int x;

    public int y;
}

输出:5 4

【讨论】:

  • @PushpitaShrestha:欢迎。
  • 这行得通。现在我想得到已经设置为单元格[3,5]的战士。我做了 cells[3,5]._warrior 但它没有返回任何东西
  • 我不明白你到底想说什么,请你详细说明..或举个例子
  • 我添加了 public Cell(Warrior Warriors) { _x = Warriors.x; _y = 战士.y; _warrior = 战士 } 这现在可以用于设置战士...感谢您的快速回复
猜你喜欢
  • 2012-02-25
  • 1970-01-01
  • 1970-01-01
  • 2012-03-09
  • 2023-04-02
  • 2013-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多