C#合并两个或多个对象
本文关键字:对象 两个 合并 | 更新日期: 2023-09-27 18:28:30
嗨,我想把两个对象合并在一起,这样我就可以用合并后的对象做其他事情。这实际上是一个简单的机场程序,我有"Route"answers"Flight",如果我想添加获取机票,我需要每个航班都在一条路线上,如果你知道我的意思的话。但请注意,我不想比较它们。如果有帮助的话,我为每个对象都有一个对象数组。
IRoute R = new LocalRoute(port, destination, routenum);
IFlight F = new LocalFlight(time, date,capacity);
我会给Flight
对象一个Route
属性。对我来说,这比创建一个同时持有航班和航线的类更有意义,因为在现实世界中,航班应该知道它必须走哪条航线。
例如:
public class Route {
public int Number { get; private set; }
public string Port { get; private set; }
public string Destination { get; private set; }
public Route(int number, string port, string destination) {
Number = number;
Port = port;
Destination = destination;
}
}
public class Flight {
public DateTime DepartureTime { get; private set; }
public int Capacity { get; private set; }
public Route Route { get; private set; }
public Flight(DateTime departureTime, int capacity, Route route) {
DepartureTime = departureTime;
Capacity = capacity;
Route = route;
}
}
然后你可以有一个可能的路线列表和一个航班列表,每个航班都有一条路线的参考。
这能满足您的需求吗?
public class FlightDetails
{
IRoute Route {get;set}
IFlight Flight {get;set;}
}
为了获得更清晰的图像,您可以执行类似的操作
public class FlightDetails
{
IRoute Route {get;set}
IFlight Flight {get;set;}
public FlightDetails(IRoute r,IFlight f)
{
this.Route = r;this.Flight = f;
}
}
IRoute R = new LocalRoute(port, destination, routenum);
IFlight F = new LocalFlight(time, date,capacity);
FlightDetails Fd = new FlightDetails(R,F);