输出中仅显示一个方法调用

时间:2015-04-02 03:52:50

标签: class object arraylist driver method-call

我试图找出如何正确调用和输出我的方法。但是,在输出中,只显示一个方法调用(最后一个)。如何让它输出所有方法调用而不仅仅是最后一个。似乎所有以前的方法调用都被覆盖,只有最后一个方法调用仍然存在。我刚刚开始使用Java。任何帮助将不胜感激。

这是PartyDriver类,我在其中进行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

1 个答案:

答案 0 :(得分:0)

没有方法,但PrintParty()打印任何东西,因为你没有告诉任何方法,但PrintParty()打印任何东西。

将输出打印到控制台的一种方法是使用System.out.println()

我已调整了您的每种方法,以便将某些内容打印到屏幕上。我还在guests类中添加了Party实例变量,并使addGuest(String guest)方法适当地修改了该实例变量。

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 ArrayList<String> guests;

    //Constructor
    public Party(int maxGuests, String host)
    {
        System.out.println("Initializing party with maxGuests: '" + maxGuests + "' and host: '" + host + "'");
        this.guests = new ArrayList<String>();
        this.maxGuests = maxGuests;
        this.host = host;
    }

    //getter
    // define type of data to be returned
    public String getHost()
    {
        System.out.println("Setting host to: " + host);
        return host;
    }

    //setter
    // setters have type void b/c they return nothing
    public void setHost(String host)
    {
        System.out.println("Setting host to: " + host);
        this.host = host;
    }

    //*************************************
    //Method to add to guest list
    public void addGuest(String guest)
    {
        if (guests.size() < maxGuests)
        {
            System.out.println("Adding guest: " + guest);
            this.guests.add(guest);
        }
        else
        {
            System.out.println("Guest list full");
        }
    }

    //*************************************
    //Method to print party
    public void printParty()
    {
        System.out.println("Guest list for " +
        this.host + "'s party is: \n\n" +
        this.guests + ".\n");
    } // end Print Party method

    public static void main(String[] args)
    {

      Party party = new Party(3, "David Beckham");
      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 class Party