非静态方法(方法name())不能从静态上下文中引用.为什么? [英] Non-static method (method name()) cannot be referenced from a static context. Why?

查看:360
本文介绍了非静态方法(方法name())不能从静态上下文中引用.为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很困惑!我有2个课程,俱乐部会员资格.在Membership中,我有 getMonth()方法,在Club中,我有 joinedMonth(),它采用参数"month"-因此用户输入一个月,然后我希望它返回在该特定月份加入的会员资格.

I'm really confused with this! I have 2 classes, Club and Membership. In Membership I have the method, getMonth(), and in Club I have joinedMonth() which takes the parameter, 'month' - so a user enters a month and then I want it to return the Membership's which joined in that specific month.

我正在尝试从类Club调用getMonth()方法,以便接下来可以比较月份的整数.但是,当我尝试调用该方法时,我得到的只是无法从静态上下文引用的非静态方法getMonth()".

I am trying to call the getMonth() method from class Club, so that I can then go on to compare the integers of the months. But, when I try to call the method, I just get the mentioned "non-static method getMonth() cannot be referenced from a static context".

基本上,这是什么,我该如何解决?

Basically, what is this and how can I resolve it?

先谢谢您!

俱乐部:

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;
    }
}

推荐答案

Membership是一个类.尽管仅在方法为静态的情况下才允许调用方法.您的getMonth方法不是静态的,因此您将需要Membership类的实例来调用它.在您的Club类中已经有一个实例列表,因此请选择其中一个实例并在其上调用getMonth.

Membership is a class. Calling methods though it is only allowed if the method is static. Your getMonth method isn't static, so you will need an instance of the Membership class to call it. You already have a list of instances in your Club class, so pick one of those and call getMonth on it.

这篇关于非静态方法(方法name())不能从静态上下文中引用.为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆