非静态方法(方法名())不能从静态上下文引用。 为什么?(Non-static method (

2019-06-25 22:46发布

我这个真糊涂! 我有2个班, 俱乐部会员 。 在会员我的方法, 得到月(),并在俱乐部我有joinedMonth(),它带有参数,“月” -所以用户输入一个月,然后我希望它返回的加入在特定月份的会员的。

我试图调用从类俱乐部的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;
    }
}

Answer 1:

Membership是一类。 调用方法虽然只被允许,如果该方法是静态的。 你getMonth方法也不是一成不变的,所以你需要的实例Membership类来调用它。 你已经在你的情况下的列表Club课,所以挑选其中的一个,并呼吁getMonth就可以了。



Answer 2:

static修饰符化妆方法/字段是类不反对(实例)的一部分。 通过使用类名作为参考调用它(或对象的引用,但是这是不好的做法)。 如果方法/字段不是静态的就必须通过参照类对象(实例)被调用。



Answer 3:

静态方法可以使用类名来访问。 在上面的代码,你想与类名成员访问的getMonth()方法(Membership.getMonth())。 但得到月()的签名是公众诠释得到月(){...},这里这个方法不包含任何static关键字。 正因为如此,你越来越“非静态方法得到月()不能从静态上下文中引用”。

为了解决这个问题,我们应该改变公众诠释得到月()公共静态INT得到月(),或使用您已经创建了成员资格类的对象。

希望这是有帮助的。



文章来源: Non-static method (method name()) cannot be referenced from a static context. Why?