我们多年来一直盯着这个问题,浪费了宝贵的时间。
我们有这些对象,我们称它们为Components。我们的应用程序允许您基于现有Component创建一个新的Component。
父级的所有值都是“继承的”(读取:可通过component.Parent
属性访问),除非您重写了一个值-例如,您想给它一个新名称,但保留其余的父级值。
// Exiting parent-child object chain
BaseObject {Id = 1, Name = "Base", Description = "Base description", Notes = "Awesome object", ParentId = null}
ChildObject1 {Id = 2, Name = "", Description = "", Notes = "", ParentId = 1}
ChildObject2 {Id = 3, Name = "Custom Name", Description = "", Notes = "", ParentId = 2}
ChildObject3 {Id = 4, Name = "", Description = "Final object", Notes = "", ParentId = 3}
现在,我想使用父级的现有值作为子级上的任何空值,从上至下将其展平。
// End result after flattening/merging
ChildObject3 {Id = 4, Name = "Custom Name", Description = "Final object", Notes = "Awesome object", ParentId = 3}
值得注意的是:没有孩子的财产,只有孩子知道父母。父母不知道孩子。
如何将这种不难看解决while(child.Parent != null)
建设和创造previousComponent
,currentComponent
等物体跟踪的一组值。
递归DeepCopy
方法怎么样?
public class ComponentClass()
{
public ComponentClass DeepCopy(ComponentClass comp)
{
CopyAllNonEmptyPropertiesTo(comp);
if (comp.StillHasEmptyProperties())
return Parent.DeepCopy(comp);
else
return comp;
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句