-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