0

我正在开发一个管理航班的小应用程序,我有一个构建航班详细信息的类和构建乘客的类,现在,我想将乘客装载到航班上,我该怎么做?我是否需要构建一个继承自这两个类的更高类并列出该类的列表(我不认为那是明智的 oop)。或者我应该在具有航班号的乘客类中添加一个票务道具,在这里是我的代码。

  public class Passenger
{
    public Passenger(string name, int passportNumber)
    {
        this.PassengerName = name;
        this.PassportNumber = passportNumber;
    }

    private string _passengerName;
    public string PassengerName
    {
        get { return _passengerName; }
        set { _passengerName = value; }
    }

    private int _passportNumber;
    public int PassportNumber
    {
        get { return _passportNumber; }
        set { _passportNumber = value; }
    }

 }

public class FlightDetails
{
    public FlightDetails(int flightNumber, string flightDestination, string planmodel)
    {
        this.FlightNumber = flightNumber;
        this.FlightDestination = flightDestination;
        this.PlanModel = planmodel;
    }

    private int _flightNumber;
    public int FlightNumber
    {
        get { return _flightNumber; }
        set { _flightNumber = value; }
    }

    private string _flightDestination;
    public string FlightDestination
    {
        get { return _flightDestination; }
        set { _flightDestination = value; }
    }

    private string _planeModel;
    public string PlanModel
    {
        get { return _planeModel; }
        set { _planeModel = value; }
    }
}
      static void Main(string[] args)
    {
        List<FlightDetails> flightList = new List<FlightDetails>();

        FlightDetails a = new FlightDetails(12,"france","jumbo");///create a flight

        flightList.Add(a);///  load up the flight

    }
4

5 回答 5

2

首先,您不能创建一个继承自其他两个类的类,因为 C# 中不允许多重继承。

您可以使用聚合,如下所示:

public class FlightDetails
{
   // ...
}

public class Passenger
{
   // ...
}

public class Flight
{
    public FlightDetails { get; private set; }
    public List<Passenger> Passengers { get; private set; }

    public Flight(FlightDetails details)
    {
        FlightDetails = details;
        Passengers = new List<Passenger>();
    }

    public AddPassenger(Passenger p)
    {
        // check for ticket and so on..

        Passengers.Add(p);
    }
}

您可以在此处阅读有关聚合的更多信息:http ://en.wikipedia.org/wiki/Object_composition#Aggregation

请注意,在此示例中,为简单起见,我使用了 List,但实际上您需要限制对该数组的访问(因为否则我可以执行以下操作:Flight.Passengers.Add(p) 而不是 Flight.AddPassenger(p))所以好主意将使用 ReadOnlyCollection 作为此列表的公共接口。

于 2013-01-16T08:27:52.550 回答
1

从逻辑上讲,FlightDetail 与 Passenger 之间的关系是 OneToMany。一个 FlightDetail 可以有多个Passenger,可以写成如下。FlightDetail 和Passenger 应该有任何共同的继承层次,因为它们没有任何共同的属性或行为。

 public class FlightDetails
 {
        private List<Passenger> passengerList;
        public void addPassenger(Passenger p){
            if(passengerList == null){
 passengerList = new ArrayList<Passenger>();

    }
    passengerList.add(p);
            }

    public List<Passenger> getPassengerList(){
    return passengerList;
    }
    //... your other detail
 }
于 2013-01-16T08:23:45.837 回答
1

您应该将 FlightDetails 属性添加到您的乘客类。这比制作一个以 PassportNumber 作为索引的列表要容易。但是,使用 List 迭代 FlightDetails 比通过Passenger 访问它更容易。

于 2013-01-16T08:24:53.913 回答
1

这是一个可能有效的示例代码。一个航班有一个或多个乘客,因此有一个乘客类型的列表。在现实生活中,乘客可以预订多个航班。如果你想要现实,你将不得不改变你的模型,但对于这种情况,它会起作用:

public class Passenger
{
    public Passenger(string name, int passportNumber)
    {
        PassengerName = name;
        PassportNumber = passportNumber
    }

    public string PassengerName { get; set; }
    public int PassportNumber { get; set; }
}

public class FlightDetails
{
    public FlightDetails(int flightNumber, string flightDestination, string planmodel)
    {
        FlightNumber = flightNumber;
        FlightDestination = flightDestination;
        PlanModel = planmodel;
        Passengers = new List<Passengers>();
    }

    public int FlightNumber { get; set; }
    public string FlightDestination  { get; set; }
    public string PlanModel { get; set; }
    public List<Passenger> Passengers { get; private set; }

    public void AddPassenger(string name, int number)
    {
        int max = 2;
        int passengersNumber = Passengers.Count;

        if (passengersNumber < max)
        {
            Passengers.Add(new Passenger(name, number);
        }
    }
public static void Main(string[] args)
{
    var flightList = new List<FlightDetails>();
    var passengersList = new List<Passenger>();

    //Add passenger-objects to passengers-list

    var flightOne = new FlightDetails(12, "France", "Jumbo");
    flightOne.Passengers = passengersList;

    flightList.Add(a);
}

这是限制乘客的更好解决方案:

public class FlightDetails
{
    public FlightDetails(int flightNumber, string flightDestination, string planmodel)
        : this(flightNumber, flightDestination, planmodel, new List<Passenger>())
    {
    }

    public FlightDetails(int flightNumber, string flightDestination, string planmodel, List<Passenger> passengers)
    {
        FlightNumber = flightNumber;
        FlightDestination = flightDestination;
        PlanModel = planmodel;

        if(passengers.Count > 2)
            //throw exception or error
        else
            _passengers = passengers;
    }

    private List<Passenger> _passengers = new List<Passenger>();

    public int FlightNumber { get; set; }
    public string FlightDestination  { get; set; }
    public string PlanModel { get; set; }

    public IEnumerable<Passenger> Passengers { get { return _passengers; } }

    public void AddPassenger(string name, int number)
    {
        int max = 2;
        int passengersNumber = _passengers.Count;

        if (passengersNumber < max)
        {
            _passengers.Add(new Passenger(name, number);
        }
    }
}

注意:此代码是在没有编译的情况下编写的。但这个想法通常是正确的。:)

于 2013-01-16T08:30:40.903 回答
0

这实际上取决于您要如何访问和存储关系。

阅读复合模式可能是个好主意,它实际上为在父子关系之间移动提供了一个很好的解决方案,即使该模式有另一个目的。

于 2013-01-16T08:29:05.377 回答