如何将对象和整数存储在数组中?
How do I store both objects and ints in an array?
我目前正在做一个项目,我必须将仓库转换为数组表示,但是,我 运行 遇到了问题。我想将工人可以行走的路径定义为值为 1 的 arraycell。工人不能去的地方的值为 0,而带有物品的货架将为 Object。这当然不是一个可行的选择,因为我们不能将对象存储在一个 int 数组中,但是,我目前想不出除了使用 int 数组之外的另一种表示路径的方法。那么如何去做呢?
这是我正在寻找的内容的快速直观表示:Array example
namespace MyApplication
{
class MagazijnObject
{
public string Locatie;//waar ligt het product?
public int Ranking; //hoe populair is dit product?
public string Item; //welk product ligt hier?
public MagazijnObject(string loc, int rank, string it)
{
Locatie = loc;
Ranking = rank;
Item = it;
}
static void Main(string[] args)
{
MagazijnObject voorbeeld = new MagazijnObject("D02020A", 1, "Meel");
Console.WriteLine(voorbeeld.Locatie);
Console.WriteLine(voorbeeld.Ranking);
Console.WriteLine(voorbeeld.Item);
int[,] GangVoorbeeld = new int[5,10]; //this is a single hallway, with shelves on either side
string gang = "D02";
//traverse array
for (int i = 0; i < GangVoorbeeld.GetLength(0); i++)
{for (int j = 0; j < GangVoorbeeld.GetLength(1); j++)
{ if ( i == 1 || i == 3) //shelves with items
{GangVoorbeeld[i,j] = new MagazijnObject(gang + i, 1, "test"); //try to create object, however can't do so because I have an int array. However, I don't know how to define the walking path in an object representation.
}
}
}
}
}}
如有任何提示,我们将不胜感激!
亲切的问候,
Douwe Brink
如果你设置在一个数组上,你可以使它成为你想要使用的一些共同父级的数组。最简单的当然是 object[][]
,但它是把你绊倒的是这种让走道成为 int
和架子成为物体的固定。将仓库中的所有东西建模为基地 class 的某种衍生品似乎更明智,而不是让人行道是 0/1 的整数..
class WarehouseEntity {}
class Wall:WarehouseEntity{}
class Walkway:WarehouseEntity{}
class Shelf:WarehouseEntity{}
var warehouse = new WarehouseEntity[5,10] ...
现在您可以在阵列中的任何位置放置墙壁、走道或架子。走道可以包含当前站在走道的 10 平方米(或其他区域)中的人员列表。一个 Shelf 可以有一个它包含的产品列表等。一堵墙做的不多,但它可以很方便地记录为一堵墙,因为也许你只想让所有类型的 WarehouseEntity 都有一个位图图形来说明它们是什么,渲染它们只是在 warehouse
数组上循环,询问每个元素的位图,而不是说“如果是墙,就画一堵墙,如果是人行道,就画人行道..”
class WarehouseEntity {
Point Coordinates {get; set;}
abstract Bitmap GetBitmap();
}
class Wall:WarehouseEntity{
override Bitmap GetBitmap(){ return _wall_bmp; }
}
class Walkway:WarehouseEntity{
public List<Person> PeopleStandingHere;
override Bitmap GetBitmap(){ return _walkway_bmp; }
}
class Shelf:WarehouseEntity{
public List<Product> ProductsOnShelf;
override Bitmap GetBitmap(){ return _walkway_bmp; }
}
...
foreach(var we in warehouse)
{
graphics.DrawImage(we.GetBitMap(), we.Coordinates.X, we.Coordinates.Y);
}
..多态,宝贝! :)
我目前正在做一个项目,我必须将仓库转换为数组表示,但是,我 运行 遇到了问题。我想将工人可以行走的路径定义为值为 1 的 arraycell。工人不能去的地方的值为 0,而带有物品的货架将为 Object。这当然不是一个可行的选择,因为我们不能将对象存储在一个 int 数组中,但是,我目前想不出除了使用 int 数组之外的另一种表示路径的方法。那么如何去做呢? 这是我正在寻找的内容的快速直观表示:Array example
namespace MyApplication
{
class MagazijnObject
{
public string Locatie;//waar ligt het product?
public int Ranking; //hoe populair is dit product?
public string Item; //welk product ligt hier?
public MagazijnObject(string loc, int rank, string it)
{
Locatie = loc;
Ranking = rank;
Item = it;
}
static void Main(string[] args)
{
MagazijnObject voorbeeld = new MagazijnObject("D02020A", 1, "Meel");
Console.WriteLine(voorbeeld.Locatie);
Console.WriteLine(voorbeeld.Ranking);
Console.WriteLine(voorbeeld.Item);
int[,] GangVoorbeeld = new int[5,10]; //this is a single hallway, with shelves on either side
string gang = "D02";
//traverse array
for (int i = 0; i < GangVoorbeeld.GetLength(0); i++)
{for (int j = 0; j < GangVoorbeeld.GetLength(1); j++)
{ if ( i == 1 || i == 3) //shelves with items
{GangVoorbeeld[i,j] = new MagazijnObject(gang + i, 1, "test"); //try to create object, however can't do so because I have an int array. However, I don't know how to define the walking path in an object representation.
}
}
}
}
}}
如有任何提示,我们将不胜感激! 亲切的问候, Douwe Brink
如果你设置在一个数组上,你可以使它成为你想要使用的一些共同父级的数组。最简单的当然是 object[][]
,但它是把你绊倒的是这种让走道成为 int
和架子成为物体的固定。将仓库中的所有东西建模为基地 class 的某种衍生品似乎更明智,而不是让人行道是 0/1 的整数..
class WarehouseEntity {}
class Wall:WarehouseEntity{}
class Walkway:WarehouseEntity{}
class Shelf:WarehouseEntity{}
var warehouse = new WarehouseEntity[5,10] ...
现在您可以在阵列中的任何位置放置墙壁、走道或架子。走道可以包含当前站在走道的 10 平方米(或其他区域)中的人员列表。一个 Shelf 可以有一个它包含的产品列表等。一堵墙做的不多,但它可以很方便地记录为一堵墙,因为也许你只想让所有类型的 WarehouseEntity 都有一个位图图形来说明它们是什么,渲染它们只是在 warehouse
数组上循环,询问每个元素的位图,而不是说“如果是墙,就画一堵墙,如果是人行道,就画人行道..”
class WarehouseEntity {
Point Coordinates {get; set;}
abstract Bitmap GetBitmap();
}
class Wall:WarehouseEntity{
override Bitmap GetBitmap(){ return _wall_bmp; }
}
class Walkway:WarehouseEntity{
public List<Person> PeopleStandingHere;
override Bitmap GetBitmap(){ return _walkway_bmp; }
}
class Shelf:WarehouseEntity{
public List<Product> ProductsOnShelf;
override Bitmap GetBitmap(){ return _walkway_bmp; }
}
...
foreach(var we in warehouse)
{
graphics.DrawImage(we.GetBitMap(), we.Coordinates.X, we.Coordinates.Y);
}
..多态,宝贝! :)