我有以下代码 struct :
我有一个Population
人的班级,上面列出了所有Humans
个人的名单.
population
类也有一个包含所有现有Genes
的列表.
一个Gene
的 struct 存储一个ID和一个值(为简单起见,只存储一个简单的字符串).
现在我想传递Human
所拥有的Gene
的ID,而不是整个Gene
struct 体,因为
多个Humans
可以具有相同的Gene
,并且这将是冗余的.
想象一下,有Genes
个不同的Genes
,Population
个IS的大小是Genes
,一个人类有5个基因.
然后,我将不得不在所有Humans
个中存储总计Genes
*Genes
*5 Genes
.
但如果我将Population
中存储的所有可用Genes
的列表传递给Human
,那么我只需要存储Genes
个基因,无论Population
有多大,Human
有多少Genes
.
但是,当Human
只有ID时,我如何检索Human
内部的Gene
的值?
internal class Population
{
List<Human> population = new List<Human>();
List<Gene> Genes = new List<Gene>();
public Population()
{
CreateGenes(10);
CreatePopulation(10);
}
private void CreateGenes(int GeneCount)
{
for (int i = 0; i < GeneCount; i++)
{
Genes.Add(new Gene() { Id = i, Value = "abc" });
}
}
private void CreatePopulation(int populationCount)
{
for (int i = 0; i < populationCount; i++)
{
population.Add(new Human() { GenID = Genes[i].Id });
}
}
}
internal class Human
{
public int GenID;
public string GeneValue()
{
// HOW DO I GET THE VALUE FROM THE LIST IN THE POPULATION?
}
}
public struct Gene
{
public int Id;
public string Value;
}