我正在制作一个管理飞行的小应用程序,我有一个班级可以建立一个航班的详细信息和班级来建造乘客,现在,我想把乘客装上飞机,我应该怎么做?我是否需要建立一个从这两个类继承的高级类,并列出该类型的类(我不认为这是明智的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
}
答案 0 :(得分: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作为此列表的公共接口。
答案 1 :(得分: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
}
答案 2 :(得分:1)
您应该向Passenger类添加FlightDetails属性。这比使用PassportNumber作为索引制作List更容易。但是,使用List迭代FlightDetails比通过Passenger访问它更容易。
答案 3 :(得分: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);
}
}
}
注意:此代码是在不编译的情况下编写的。但这个想法通常是正确的。 :)
答案 4 :(得分:0)
这实际上取决于您希望如何访问和存储关系。
阅读composite pattern可能是一个好主意,它实际上有一个很好的解决方案,可以在父子关系之间旅行,即使模式有其他目的。