C# 实现递归聚合
我正在创建一个应用程序,它需要在其中一个类中使用递归聚合。手头的类被实体化为“组件”,组件可以由存储在列表中的子组件组成。下面的代码显示了这一点C# 实现递归聚合,c#,aggregation,C#,Aggregation,我正在创建一个应用程序,它需要在其中一个类中使用递归聚合。手头的类被实体化为“组件”,组件可以由存储在列表中的子组件组成。下面的代码显示了这一点 public class Component { //vars for class public String componentName; public String componentShape; public String componentColour; public String componentMaterial; public i
public class Component {
//vars for class
public String componentName;
public String componentShape;
public String componentColour;
public String componentMaterial;
public int numChildComps;
//list to store child components of component
public List<Component> childComponents;
public Component(string _componentName, string _componentShape, string _componentColour, string _componentMaterial, int _numChildComps)
{
componentName = _componentName;
componentShape = _componentShape;
componentColour = _componentColour;
componentMaterial = _componentMaterial;
numChildComps = _numChildComps;
//if component has no child components set list to null
if (numChildComps == 0)
{
//instatiate new list
childComponents = new List<Component>();
childComponents = null;
}
else if(numChildComps != 0)//if not null then create child components for the amount stated above.
{
childComponents = new List<Component>();
for (int i = 0; i < numChildComps; i++)
{
Console.WriteLine("Add details for child component " + (i+1));
Console.WriteLine("Enter component Name: ");
string name = Console.ReadLine();
Console.WriteLine("Enter shape: ");
string shape = Console.ReadLine();
Console.WriteLine("Enter Colour: ");
string colour = Console.ReadLine();
Console.WriteLine("Enter Material: ");
string material = Console.ReadLine();
Console.WriteLine("Enter num child components: ");
string num = Console.ReadLine();
childComponents.Add(new Component(name, shape, colour, material, Int16.Parse(num)));//instatiate new child component with params and add to the list.
}
}
}
显然,这可能会永远持续下去,我已经尝试过创建一段代码来停用所有组件和子组件,但它没有起作用,因为您需要知道一个模型拥有的组件总数,然后是组件和子组件,等等
我尝试过的代码(不模拟上面的示例)
IEnumerable moderes=来自数据库中的SEModel sm
选择sm;
foreach(modelres中的SEModel项)
{
Console.WriteLine(item.getSetModelName);
Console.WriteLine(item.componentList.First().componentName);
foreach(item.componentList中的子组件citem)
{
Console.WriteLine(citem.childComponents.First().componentName);
foreach(citem.childComponents中的子组件科学)
{
Console.WriteLine(scitem.componentName);
}
}
如上所述,您必须知道包含childcomponents的组件的数量,然后是它们的childcomponents等等。如果您需要以平面列表的形式检索组件,您可以这样做:
public List<Component> GetAllComponents(List<Component> components)
{
var result = new List<Component>();
result.AddRange(components);
foreach (var component in components)
result.AddRange(GetAllComponents(component.childComponents));
return result;
}
var allComponents = GetAllComponents(Model.Components);
公共列表GetAllComponents(列表组件)
{
var result=新列表();
结果:添加范围(组件);
foreach(组件中的var组件)
AddRange(GetAllComponents(component.childComponents));
返回结果;
}
var allComponents=GetAllComponents(Model.Components);
虽然从你的问题我不确定你到底想做什么。public IEnumerable GetComponents()
public IEnumerable<Component> GetComponents()
{
yield return this;
foreach( var childComp in childComponents )
{
foreach( var comp in childComp.GetComponents() )
{
yield return comp;
}
}
}
{
收益回报这一点;
foreach(childComponents中的变量childComp)
{
foreach(childComp.GetComponents()中的var comp)
{
收益率报酬率;
}
}
}
如果你不确定什么是
yield-return
,请使用不起作用的递归来显示你的代码。如果你知道组件的总量,所有子组件都在下面,它们的子组件等等,那么它就起作用了。这并不理想,因为每个模型的组件数量会有所不同el.考虑到OP与递归的斗争,我不认为yield return
是合适的。:)但你的代码确实是最干净的解决方案。嘿,它是递归的!;)+1好的解决方案,总结一下yield return object
是什么和做什么,它是实现枚举器时迭代器方法中使用的上下文关键字。Basic实际上,它有两个用例:yield return obj;
返回序列中的下一项。yield break;
停止返回序列元素(如果控件到达迭代器方法体的末尾,这会自动发生).在环顾四周并阅读了这篇文章后,非常感谢您的意见,也感谢所有其他人的意见。
public List<Component> GetAllComponents(List<Component> components)
{
var result = new List<Component>();
result.AddRange(components);
foreach (var component in components)
result.AddRange(GetAllComponents(component.childComponents));
return result;
}
var allComponents = GetAllComponents(Model.Components);
public IEnumerable<Component> GetComponents()
{
yield return this;
foreach( var childComp in childComponents )
{
foreach( var comp in childComp.GetComponents() )
{
yield return comp;
}
}
}