c#-具有多个派生对象的列表.如何访问派生字段
作者:互联网
目前,我正在使用一个名为“ JourneyLeg”的基类.这个基类有5个派生类,所有派生类都继承自该基类.这些类中的两个称为“ WalkingLeg”和“ VehicleLeg”.这2个派生类均包含“从”和“到”字段.其他3个没有.
List<JourneyLeg> legs
现在,我有了包含所有派生对象的列表.其中一些是“步行腿”,有些是“车辆腿”,其余的是其他三个派生类之一.该列表如上定义.
我想遍历完整列表,仅对步行和车辆对象执行操作.这些动作包括访问“从”和“到”.这2个字段仅在这2个派生类中可用,而在基类中不可用.
我能想到的唯一方法是检查它是否是2个派生类之一,然后执行操作(请参见下文).但是这样我有很多重复的代码.我认为我无法在方法中提取重复的代码,因为那时我们提供给该方法的参数对象将是VehicleLeg或WalkingLeg,并且不能两者都存在.
case VehicleLeg vehicleLeg:
{
var legTo = new DirectionsRequestJourneyLegsLocation(vehicleLeg.To.LatLong, vehicleLeg.To.IsVisible);
var legFrom = new DirectionsRequestJourneyLegsLocation(vehicleLeg.From.LatLong, vehicleLeg.From.IsVisible);
directionsRequestJourneyleg.id = vehicleLeg.Id;
directionsRequestJourneyleg.From = legFrom;
directionsRequestJourneyleg.To = legTo;
directionsRequestJourneyleg.LegArrival = vehicleLeg.From.Time.Planned;
directionsRequestJourneyleg.LegDeparture = vehicleLeg.To.Time.Planned;
directionsRequestJourneyleg.Type = DirectionsRequestJourneyLegType.Vehicle;
directionsRequestJourneyleg.Modality = vehicleLeg.Modality;
break;
}
case WalkingLeg walkingLeg:
{
var legTo = new DirectionsRequestJourneyLegsLocation(walkingLeg.To.LatLong, walkingLeg.To.IsVisible);
var legFrom = new DirectionsRequestJourneyLegsLocation(walkingLeg.From.LatLong, walkingLeg.From.IsVisible);
directionsRequestJourneyleg.id = walkingLeg.Id;
directionsRequestJourneyleg.From = legFrom;
directionsRequestJourneyleg.To = legTo;
directionsRequestJourneyleg.LegArrival = walkingLeg.From.Time.Planned;
directionsRequestJourneyleg.LegDeparture = walkingLeg.To.Time.Planned;
directionsRequestJourneyleg.Type = DirectionsRequestJourneyLegType.Walking;
break;
}
我可能在这里错过了一些简单的事情.但是我不能把头缠住它.我希望有人能让我回到正确的轨道上.
下面是这些类:
/// <summary>
/// JourneyLeg is the base class used to define commonalities between:
///
/// *VehicleLeg, WalkingLeg, TransitionLeg, AdvertisementLeg*
/// </summary>
public class JourneyLeg
{
[Required]
public string Id { get; set; }
[Required]
public LegType Type { get; set; }
}
/// <summary>
/// Vehicle leg model
/// </summary>
public class VehicleLeg : JourneyLeg
{
[Required]
public ModalityType Modality { get; set; }
[Required]
public LegStop From { get; set; }
[Required]
public LegStop To { get; set; }
}
/// <summary>
/// Walking leg model
/// </summary>
public class WalkingLeg : JourneyLeg
{
[Required]
public LegStop From { get; set; }
[Required]
public LegStop To { get; set; }
/// <summary>
/// Displays the total walk time in minutes and in parenthese the distance in meters
/// </summary>
[Required]
public string Description { get; set; }
}
解决方法:
我只是添加一个中间类,该中间类添加From和To属性:
public class JourneyLeg
{
[Required]
public string Id { get; set; }
[Required]
public LegType Type { get; set; }
}
// You may want another name...
public class JourneyFromToLeg : JourneyLeg
{
[Required]
public LegStop From { get; set; }
[Required]
public LegStop To { get; set; }
}
public class VehicleLeg : JourneyFromToLeg
{
[Required]
public ModalityType Modality { get; set; }
}
public class WalkingLeg : JourneyFromToLeg
{
[Required]
public string Description { get; set; }
}
因此,您可以使用:
case JourneyFromToLeg fromToLeg:
{
var legTo = new DirectionsRequestJourneyLegsLocation(fromToLeg.To.LatLong, fromToLeg.To.IsVisible);
var legFrom = new DirectionsRequestJourneyLegsLocation(fromToLeg.From.LatLong, fromToLeg.From.IsVisible);
directionsRequestJourneyleg.id = fromToLeg.Id;
directionsRequestJourneyleg.From = legFrom;
directionsRequestJourneyleg.To = legTo;
directionsRequestJourneyleg.LegArrival = fromToLeg.From.Time.Planned;
directionsRequestJourneyleg.LegDeparture = fromToLeg.To.Time.Planned;
// you may want to think a little more of this too
if (fromToLeg is WalkingLeg)
{
directionsRequestJourneyleg.Type = DirectionsRequestJourneyLegType.Walking;
}
else
{
directionsRequestJourneyleg.Type = DirectionsRequestJourneyLegType.Vehicle;
directionsRequestJourneyleg.Modality = ((VehicleLeg)fromToLeg).Modality;
}
break;
}
如果您更喜欢使用接口继承,则可以使用IFromLeg / IToLeg / IFromToLeg接口
标签:base-class,derived-class,c 来源: https://codeday.me/bug/20191024/1924027.html