我有大约10个类,每个类都有一个LUMP_INDEX和SIZE的静态常量。 我想要每个这些类的数组,其中数组的大小是使用这两个常量计算出来的。 目前,我为每个类都有一个创建数组的函数,大概是这样的:
private Plane[] readPlanes()
{
int count = header.lumps[Plane.LUMP_INDEX].filelen / Plane.SIZE;
Plane[] planes = new Plane[count];
for(int i = 0; i < count; i++)
planes[i] = new Plane();
return planes;
}
private Node[] readNodes()
{
int count = header.lumps[Node.LUMP_INDEX].filelen / Node.SIZE;
Node[] nodes = new Node[count];
for(int i = 0; i < count; i++)
nodes[i] = new Node();
return nodes;
}
private Leaf[] readLeaves()
{
int count = header.lumps[Leaf.LUMP_INDEX].filelen / Leaf.SIZE;
Leaf[] leaves = new Leaf[count];
for(int i = 0; i < count; i++)
leaves[i] = new Leaf();
return leaves;
}
等等。 有10个这样的函数,唯一的区别是类类型,所以你可以看到,有很多重复。
有没有人有什么想法来避免这种重复? 谢谢。 (我之前问过类似的问题,但我猜我问错了)
header
对象是什么? - Brian Roach