gpt4 book ai didi

java - 将成员对象添加到 ArrayList

转载 作者:行者123 更新时间:2023-12-01 11:03:13 28 4
gpt4 key购买 nike

我正在尝试完成一个程序来帮助我在类里面学习 Java 之前学习它。但我在尝试向俱乐部添加成员(member)、打印出来然后计算成员(member)数量时遇到了困难。将基础知识添加到代码中后,我不知道从哪里开始。

我是堆栈溢出新手,不知道如何正确格式化,所以很抱歉。 :)任何提示和帮助将不胜感激。谢谢

package lab8.club;

import java.util.ArrayList;


public class Club
{

ArrayList<Membership> members;
/**
* Constructor for objects of class Club
*/
public Club()
{
members = new ArrayList<Membership>();
// Initialise any fields here ...

}

/**
* Add a new member to the club's list of members.
* @param member The member object to be added.
*/
public void join(Membership member)
{
members.add(member);
}

/**
* @return The number of members (Membership objects) in
* the club.
*/
public int numberOfMembers()
{
return members.size();
}

public static void main(String args[]){
Membership member1 = new Membership("test", 1, 2011);

System.out.println();


}
}


package lab8.club;

public class Membership
{
// The name of the member.
private String name;
// The month in which the membership was taken out.
private int month;
// The year in which the membership was taken out.
private int year;


public Membership(String name, int month, int year)
throws IllegalArgumentException
{
if(month < 1 || month > 12) {
throw new IllegalArgumentException(
"Month " + month + " out of range. Must be in the range 1 ... 12");
}
this.name = name;
this.month = month;
this.year = year;
}


public String getName()
{
return name;
}


public int getMonth()
{
return month;
}


public int getYear()
{
return year;
}


public String toString()
{
return "Name: " + name +
" joined in month " +
month + " of " + year;
}
}

package lab8.club;


public class ClubDemo
{
// instance variables - replace the example below with your own
private Club club;

/**
* Constructor for objects of class ClubDemo
*/
public ClubDemo()
{
club = new Club();
}

/**
* Add some members to the club, and then
* show how many there are.
* Further example calls could be added if more functionality
* is added to the Club class.
*/
public void demo()
{
club.join(new Membership("David", 2, 2004));
club.join(new Membership("Michael", 1, 2004));
System.out.println("The club has " +
club.numberOfMembers() +
" members.");
}
}

最佳答案

你的代码实际上没问题。您真正需要的是一个开始执行代码的 main 方法。创建一个新类,例如以下 Demo.java:

public class Demo {
public static void main(String[] args) {
ClubDemo demo = new ClubDemo();
demo.demo();
}
}

输出:

The club has 2 members.

您将看到,在 ClubDemodemo() 方法中,通过调用 ArrayList.addclub.join 添加成员。方法将成员对象添加到 members ArrayList

有关 main 方法签名的更多信息,请参阅 Java Hello World Tutorial :

In the Java programming language, every application must contain a main method whose signature is:

public static void main(String[] args)

The modifiers public and static can be written in either order (public static or static public), but the convention is to use public static as shown above. You can name the argument anything you want, but most programmers choose "args" or "argv".

关于java - 将成员对象添加到 ArrayList<Membership>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33176199/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com