C#对象层次结构

The*_*aut 1 c# object

我有一个单元格列表,每个列表都有一个站点列表.

有时我需要获得一个站的父细胞.

我该如何实现这种层次结构?

  • 我应该将父单元格保留为工作站对象中的属性吗?
  • 我应该只保留工作站对象中的父单元ID吗?
  • 做点什么不一样?

pla*_*ton 5

如果我是你,我会创建两个类:

public class Cell {
 ...
  public List<Station> Stations {
    get {...}
  }

  protected Station AddStation() {
     Station result = new Station(this);
     Stations.Add(result);
     return result;
  }
}

public class Station {
  public Station(Cell cell) {
    this.cell = cell;
  }
  Cell cell;
  public Cell Cell {get {return cell;}}
}
Run Code Online (Sandbox Code Playgroud)

拥有此结构后,您始终可以从Station对象访问Cell.