继承需要存储子类特定数据的数组的最佳方法是什么?
我正在尝试设置类似于以下内容的继承层次结构:
abstract class Vehicle
{
public string Name;
public List<Axle> Axles;
}
class Motorcycle : Vehicle
{
}
class Car : Vehicle
{
}
abstract class Axle
{
public int Length;
public void Turn(int numTurns) { ... }
}
class MotorcycleAxle : Axle
{
public bool WheelAttached;
}
class CarAxle : Axle
{
public bool LeftWheelAttached;
public bool RightWheelAttached;
}
我只想将 MotorcycleAxle 对象存储在 Motorcycle 对象的 Axles 数组中,将 CarAxle 对象存储在 Car 对象的 Axles 数组中。 问题是没有办法重写子类中的数组来强制其中之一。 理想情况下,类似以下内容对于 Motorcycle 类有效:
class Motorcycle : Vehicle
{
public override List<MotorcycleAxle> Axles;
}
但覆盖时类型必须匹配。 我怎样才能支持这个架构? 我是否只需要在访问 Axles 成员的地方进行大量运行时类型检查和转换? 我不喜欢添加运行时类型检查,因为您开始失去强类型和多态性的好处。 在这种情况下必须至少进行一些运行时检查,因为 WheelAttached 和 Left/RightWheelAttached 属性取决于类型,但我想最小化它们。
I'm trying to set up an inheritance hierarchy similar to the following:
abstract class Vehicle
{
public string Name;
public List<Axle> Axles;
}
class Motorcycle : Vehicle
{
}
class Car : Vehicle
{
}
abstract class Axle
{
public int Length;
public void Turn(int numTurns) { ... }
}
class MotorcycleAxle : Axle
{
public bool WheelAttached;
}
class CarAxle : Axle
{
public bool LeftWheelAttached;
public bool RightWheelAttached;
}
I would like to only store MotorcycleAxle objects in a Motorcycle object's Axles array, and CarAxle objects in a Car object's Axles array. The problem is there is no way to override the array in the subclass to force one or the other. Ideally something like the following would be valid for the Motorcycle class:
class Motorcycle : Vehicle
{
public override List<MotorcycleAxle> Axles;
}
but the types have to match when overriding. How can I support this architecture? Will I just have to do a lot of run-time type checking and casting wherever the Axles member is accessed? I don't like adding run-time type checks because you start to lose the benefits of strong typing and polymorphism. There have to be at least some run-time checks in this scenario since the WheelAttached and Left/RightWheelAttached properties depend on the type, but I would like to minimize them.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用更多泛型
Use more generics
我脑海中浮现出两个选择。 第一个是使用泛型:
第二个使用遮蔽 - 这假设你有属性:
遮蔽的问题是你有一个通用列表。 不幸的是,您不能将列表限制为仅包含 CarAxle。 另外,您不能将 List投射为 进入列表- 即使那里有一个继承链。 您必须将每个对象转换为一个新的列表(尽管使用 LINQ 会变得更容易)。
我自己会选择仿制药。
2 options spring to mind. 1 is using generics:
The second uses shadowing - and this assumes you have properties:
The problem with shadowing is that you have a generic List. Unfortunately, you can't constrain the list to only contain CarAxle. Also, you can't cast a List<Axle> into List<CarAxle> - even though there's an inheritance chain there. You have to cast each object into a new List (though that becomes much easier with LINQ).
I'd go for generics myself.
我问了一个类似的问题并且得到了更好的答案,问题与C#对协变和逆变的支持有关。 请参阅该讨论以获取更多信息。
I asked a similar question and got a better answer, the problem is related to C#'s support for covariance and contravariance. See that discussion for a little more information.