因此可以選擇Section和Item。創建一個接口,他們都將實施:
interface ISelectable
{
void Select();
void DeSelect();
}
現在,貨號:
class Item implements ISelectable
{
private ISelectable _section; // The object that contains this item
private bool _selected;
public Item()
{
_selected = false;
}
public void Select()
{
_selected = true;
_section.Select();
}
public void DeSelect()
{
_selected = false;
_section.DeSelect();
}
public void setSection(ISelectable section)
{
_section = section;
}
}
所以,當我們[德]選擇一個項目,它的父(段)也[德]中選擇。該科:
class Section implements ISelectable
{
private bool _selected;
public ArrayList Items;
public Section()
{
_selected = false;
Items = new ArrayList();
}
public void addItem(Item i)
{
i.setSection(this);
Items.add(i);
}
public void Select()
{
_selected = true;
}
public void DeSelect()
{
_selected = false;
}
}
到目前爲止,我們已經建立了一個系統,當選擇它的一個孩子,將選擇父。
可以定義使用「規則/模式」
private PropertyType _propertyName;
public void setPropertyName(PropertyType propertyName)
{
_propertyName = propertyName;
}
public PropertyType getPropertyName()
{
return _propertyName;
}
爲科和項目特定的屬性,這可能更好地工作,你給一個真實的使用情況下,如果正是你正在嘗試做的。 – PeeHaa