假设我有 carbase,它代表所有汽车(如本田、丰田、福特等)的基础等级。)。同样,我也有卡车基地(所有不同的车辆类别)。另请注意,每辆 honda 车辆都有一些由 hondabaseobject 代表的共同属性。
现在,我想定义一个 HondaCivic;理想情况下是这样hondacivic : hondacar, hondabaseobject
,但由于 .net 不允许多重继承,我正在考虑各种建模方法。
对象 -
- 我想访问所有带有本田基础对象的本田车辆,所有带有汽车底座的汽车,所有带有卡车底座的卡车等......
请参阅我提出的以下模型。我可以接受,但由于某种原因我不喜欢我定义的接口(IHondaBaseObject)
我刚刚定义了它,以便我可以用它处理所有本田车辆,因为我知道所有本田车辆都组成本田基础对象并实施此合同。
有没有更好的处理方法?
可能类似于HondaCivic<HondaBaseObject> : HondaCar
- 但处理很困难,因为我不能简单地转换HondaCivic<HondaBaseObject> as HondaBaseObject
- 使用接口我可以简单地交换类型。
看起来它类似于C# 中的多重继承,基本上在 C# 中实现了多重继承
即 HondaCivic : CarBase, HondaBaseObject <=> HondaCivic : CarBase, IHondabaseObject { HondaBaseObject Honda; }
class vehicle
{
}
/// <summary>
/// Base class for all the cars (common fields and operations of car)
/// </summary>
class CarBase : vehicle
{
//parts
virtual steering steering {get;}
virtual void StartingTheCar(...)
//engine
}
class HondaCar : CarBase
{
//overrides
//specializations
}
class ToyotaCar : CarBase
{
//overrides
//specializations
}
class TruckBase : vehicle
{
//overrides
//specializations
}
class HondaTruck : TruckBase
{
//overrides
//specializations
}
class ToyotaTruck : TruckBase
{
//overrides
//specializations
}
//Is it necessary? - i introduces this so that as i want to access all types of hoda vehicles with a single type
//but having interface, just for the sake of handling doesnt make sense?
//i am not sure - but, looks like something is wrong.
interface IHondaBaseObject
{
}
/// <summary>
/// Object which contains the common attributes for each honda vehicle (say cars, suvs, trucks, bikes etc...)
/// </summary>
class HondaBaseObject
: IHondaBaseObject
{
string identity { get; set; }
public override bool Equals(object obj)
{
return base.Equals(obj);
}
public override int GetHashCode()
{
return base.GetHashCode();
}
}
//Now what is the best way to model it? should i use generics? or just a simple stragey pattern?
class HondaCivic : HondaCar, IHondaBaseObject
{
HondaCivic(HondaBaseObject ho)
{
}
public HondaBaseObject { get; }
}