什么是关联类,如何为这个给定的模型实现关联类。 查看屏幕截图
请您参考如下方法:
基本上:
class GeoObjectPart
{
public int Category { get; set; }
public IEnumerable<GeoObjectPart> Parts { get; set; }
}
class GeoObject
{
public int ObjectType { get; set; } // do not use Type as a property name!
public long Id { get; set; }
public IEnumerable<GeoObjectPart> Parts { get; set; }
}
并且Line, Point, Area继承了GeoObjectPart类。
顺便说一句,GeoObject.Type 和GeoObjectPart.Category 意思一样吗?在这种情况下,请考虑进行一些重新设计,因为您可以使用单个类而不是同时使用 GeoObject 和 GeoObjectPart
此外,一些建议:
- 将基类标记为抽象类,避免实例化设计为基类的类
- 以
base为前缀命名基类,如GeoObjectBase - 考虑
GeoObjectBase类的接口(interface) - 如果可能的话,避免使用
public setters,这样实例就会是不可变的
