包含多个派生对象的列表。如何访问派生中的字段

List with multiple derived object. How to acces the fields in the derived

目前我正在使用名为 "JourneyLeg" 的 Base class。这个基 class 有 5 个派生,它们都继承自基 class。其中两个 classes 称为 "WalkingLeg""VehicleLeg"。这 2 个派生的 classes 都包含一个 "from""to" 字段。其他3个没有。

List<JourneyLeg> legs

我现在有了包含所有派生对象的列表。其中一些是 Walkinleg,一些是 vehicleleg,其余的是其他 3 个派生的 classes 之一。列表定义如上。

我想遍历整个列表并只对步行和车辆对象执行操作。这些操作包括访问“from”和"to"。这 2 个字段仅在这 2 个派生 classes 中可用,在基础 class.

中不可用

我能想到的唯一方法是检查它是否是 2 个派生的 classes 之一,然后执行操作(见下文)。但是这样我就有了很多重复的代码。我想我不能提取方法中的重复代码,因为那样我们给这个方法的参数对象将是 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;
}

我可能在这里遗漏了一些简单的东西。但我无法理解它。我希望有人可以让我回到正确的轨道上。

下面是 classes:

/// <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; }
}

创建一个新的 class JourneyLegFromTo 或继承 JourneyLeg 的类似物,然后使 VehicleLegWalkingLeg 继承新的 class .

然后您可以将代码减少到检查它是否是 JouneyLegFromTo 而不是这两种情况,您将能够访问 from 和两个字段。

public class JourneyLegFromTo : JourneyLeg
{
    [Required]
    public LegStop From { get; set; }
    [Required]
    public LegStop To { get; set; }
}

public class WalkingLeg : JourneyLegFromTo 
{
    ...
}

public class VehicleLeg : JourneyLegFromTo 
{
    ...
}

稍后:

case JourneyLegFromTo journeyLegFromTo:
{
    var legTo = new DirectionsRequestJourneyLegsLocation(journeyLegFromTo.To.LatLong, journeyLegFromTo.To.IsVisible);
    var legFrom = new DirectionsRequestJourneyLegsLocation(journeyLegFromTo.From.LatLong, journeyLegFromTo.From.IsVisible);
    ...

我只想添加一个中介 class 来添加 FromTo 属性:

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 接口