我真的很困惑这个!我有 2 个班级,俱乐部和会员资格。在会员资格中,我有方法getMonth(),在俱乐部中,我加入了接受参数“月”的方法 - 所以用户输入一个月,然后我希望它返回加入该特定月份的会员资格。
我正在尝试从 Club 类中调用 getMonth() 方法,这样我就可以继续比较月份的整数。但是,当我尝试调用该方法时,我只是得到提到的“不能从静态上下文引用非静态方法 getMonth()”。
基本上,这是什么,我该如何解决?
先感谢您!
俱乐部:
public class Club
{
private ArrayList<Membership> members;
private int month;
/**
* Constructor for objects of class Club
*/
public Club()
{
// 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();
}
/**
* Determine the number of members who joined in the given month
* @param month The month we are interested in.
* @return The number of members
*/
public int joinedMonth(int month){
Membership.getMonth();
}
}
会员资格:
public class Membership
{
// The name of the member.
private String name;
// The month in which the membership was taken out.
public int month;
// The year in which the membership was taken out.
private int year;
/**
* Constructor for objects of class Membership.
* @param name The name of the member.
* @param month The month in which they joined. (1 ... 12)
* @param year The year in which they joined.
*/
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;
}
/**
* @return The member's name.
*/
public String getName()
{
return name;
}
/**
* @return The month in which the member joined.
* A value in the range 1 ... 12
*/
public int getMonth()
{
return month;
}
/**
* @return The year in which the member joined.
*/
public int getYear()
{
return year;
}
/**
* @return A string representation of this membership.
*/
public String toString()
{
return "Name: " + name +
" joined in month " +
month + " of " + year;
}
}