-1
我想弄清楚如何調用並正確輸出我的方法。但是,在輸出中,只顯示一個方法調用(最後一個)。我怎樣才能讓它輸出所有的方法調用,而不僅僅是最後一個。似乎所有以前的方法調用都被覆蓋,只有最後一個方法調用仍然存在。我剛剛開始使用Java。任何幫助,將不勝感激。只有一個方法調用顯示在輸出
這裏是派對驅動類,我做5個方法調用。只有最後一個顯示在printParty方法中。
import java.util.ArrayList;
public class PartyDriver
{
public static void main(String[] args)
{
Party party = new Party(3, "David Beckham");
ArrayList<String> guest = new ArrayList<>();
party.addGuest("Roberto Baggio");
party.addGuest("Zinedine Zidane");
party.addGuest("Roberto Baggio");
party.addGuest("Johan Cruyff");
party.addGuest("Diego Maradona");
party.printParty();
} // end main
}//End Party Driver
這裏是黨類與所有的我的方法:
import java.util.ArrayList;
public class Party
{
// instance variables that will hold your data
// private indicates they belong to this class exclusively
private int maxGuests;
private String host;
private String guest;
//Constructor
public Party(int maxGuests, String host)
{
this.maxGuests = maxGuests;
this.host = host;
}
//getter
// define type of data to be returned
public String getHost()
{
return host;
}
//setter
// setters have type void b/c they return nothing
public void setHost(String host)
{
this.host = host;
}
//*************************************
//Method to add to guest list
public void addGuest(String guest)
{
this.guest = guest;
}
//*************************************
//Method to print party
public void printParty()
{
System.out.println("Guest list for " +
this.host + "'s party is: \n\n" +
this.guest + ".\n");
} // end Print Party method
}//end class Party
好的,我會嘗試你提出的這個建議。但是我所說的是我的printParty方法應該返回的不僅僅是一個名字。假設返回3個名字。它只是返回最後一個名字是「迭戈馬拉多納」。我調用了addGuest方法5次。但只有一個名字(「Diego Maradona」)似乎被添加到Guest方法中。我無法弄清楚爲什麼其他名稱不會被添加到來賓列表中,因爲它們也是printParty方法的一部分。 printParty方法應該打印來賓列表中的所有名稱,並且只打印一個名稱。 – Fryguy 2015-04-02 12:28:41