C#合并两个或多个对象

时间:2013-08-22 06:56:38

标签: c#

您好我想合并两个对象,以便我可以使用合并的对象做其他事情。实际上是一个简单的机场程序,我有“路线”和“飞行”,如果我想添加获取票我如果你知道我的意思,就需要每个航班都在路线上。但请注意我不想比较它们。如果它有帮助,我会为每个航班提供一系列物体。

 IRoute R = new LocalRoute(port, destination, routenum);
 IFlight F = new LocalFlight(time, date,capacity);

3 个答案:

答案 0 :(得分:2)

我会给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;
    }
}

然后,您可以列出可能的路线和航班列表,每个航班都有一条路线参考。

答案 1 :(得分:1)

这会做你想要的吗?

public class FlightDetails
{
    IRoute Route {get;set}
    IFlight Flight {get;set;}
}

答案 2 :(得分:0)

为了更清晰的图片,你可以做这样的事情

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);