我對Java很新,對編程也相對陌生。我有3個班,這是連接。Java語法方法和類
1類:飛行
public class Flight {
private int flightNumber;
Passenger passenger1 = null; // two objects?
Passenger passenger2 = null;
private int confirmedPassengers = 0;
Passenger bagsChecked;
//private boolean bagsLoaded;
public Flight(int fn, int cf){
flightNumber = fn;
//passenger1 = p1;
//passenger2 = p2;
confirmedPassengers = cf;
//bagsChecked = bc;
}
public boolean addPassenger(Passenger p){
if (confirmedPassengers == 0){
passenger1= p;
confirmedPassengers+=1;
System.out.println("P1 has been added.");
return true;
}
if (confirmedPassengers == 1){
passenger2 = p;
confirmedPassengers+=1;
System.out.println("P2 has been added");
return true;
}
else{
System.out.println("This passenger was not added because the capacity is full.");
return false;
}
}
}
2類:乘客
public class Passenger {
private String name;
Flight flight;
private int bagsChecked;
private String confirmationNumber;
public Passenger(String n, Flight f, int b, String cn){
name = n;
flight = f;
bagsChecked = b;
confirmationNumber = cn;
}
public boolean hasConfirmation(){
if (confirmationNumber != null) {
return true;
}
else{
return false;
}
}
}
3類:亭:
public class Kiosk {
private int kioskNumber;
public Kiosk (int k){
kioskNumber = k;
}
public void checkIn(Passenger p){
if (p.hasConfirmation() == true) {
addPassenger(p); // trying to add Passenger here
System.out.println("Passenger " + p.getName() + ""+ "added in at Kiosk1 ");
}
}
}
我tryi如果乘客在Kiosk
處登記,則添加Passenger
至Flight
。我如何在Flight
中使用addPassenger
方法將Passenger
添加到Kiosk
?
Java,而不是JAVA。 – Nishant
Kisok類的功能是什麼?我錯過了什麼嗎? –
因爲你是初學者,所以給予建議.... 每當你參考時,首先檢查'null'。否則你的程序將崩潰。因此,在'checkIn'方法中,您應該將 'if if(p == null){*處理這種情況*/ }' – shan