gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 15:14:52 24 4
gpt4 key购买 nike

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

List<JourneyLeg> legs

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

我想遍历整个列表并只对步行和车辆对象执行操作。这些操作包括访问“”和“到”。这 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; }
}

最佳答案

我只想添加一个添加 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;
}

如果您更愿意使用接口(interface)继承,您可以使用 IFromLeg/IToLeg/IFromToLeg 接口(interface)

关于c# - 包含多个派生对象的列表。如何访问派生中的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53580647/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com