编程时遇到对象关联api的设计问题,感觉可能这种api的设计方案本身就有问题,所以目前还未有解决方案。
问题如下:
using system;
using system.collections.generic;
using system.linq;
using system.text;
namespace consoleapplication
{
/// <summary>
/// 在notimplement的方法/属性中填入相应内容,以保证程序不会抛出异常。
/// (就是:parent属性,addchild方法,removechild方法。)
///
/// 不能修改其它代码。
/// </summary>
class program
{
static void main(string[] args)
{
parent parenta = new parent();
parent parentb = new parent();
child child = new child();
parenta.addchild(child);
child.parent = parentb;
assert(parenta.children.count == 0, parentb.children.count == 1, child.parent == parentb);
assert(parentb.children.count == 0, parenta.children.count == 1, child.parent == parenta);
child.parent = null;
assert(parentb.children.count == 0, parenta.children.count == 0, child.parent == null);
}
static void assert(params bool[] values)
foreach (var value in values)
{
if (value == false)
{
throw new exception();
}
}
}
class parent
private list<child> _children;
public parent()
this._children = new list<child>();
public ilist<child> children
get
return new system.collections.objectmodel.readonlycollection<child>(this._children);
public void addchild(child child)
throw new notimplementedexception();
public void removechild(child child)
class child
private parent _parent;
public parent parent
throw new notimplementedexception();
set
}