Java非静态方法addInv

Java非静态方法addInv

本文介绍了Java非静态方法addInv(int)不能从静态上下文引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个错误非常好,但这是一次我被困住。我知道我不能从main方法调用非静态变量,但这是令人困惑。也许你可以帮助?该错误显示在 addInv(1); line ...

I know this error very well however this is the once time I'm stumped. I know that I can't call non-static variables from main method but this is confusing. Maybe you can help? That error is show on addInv(1); line...

代码:

import java.io.*;
import java.util.*;
import javax.swing.*;

public class item
{
    public static int attack, defense;
    public static ArrayList<String> arr;
public static String name, desc, typeOf, attackAdd, defenseAdd, canSell, canEat,earnedCoins,canEquip;

    String stats[];

        public static void main(String args[])
        {

            addInv(1);

        }

public void addInv(int e) {

String iname = getItem(1)[0];

String idesc = getItem(1)[1];
int itypeOf = Integer.parseInt(getItem(1)[2]);
int iattackAdd = Integer.parseInt(getItem(1)[3]);
int idefenseAdd = Integer.parseInt(getItem(1)[4]);
boolean icanSell = Boolean.parseBoolean(getItem(1)[5]);
boolean icanEat = Boolean.parseBoolean(getItem(1)[6]);
int iearnedCoins = Integer.parseInt(getItem(1)[7]);


attack = attack + iattackAdd;
defense = defense + idefenseAdd;
System.out.println("You picked up: " + iname);
arr.add("dan");
System.out.println("Description: " + idesc);

}

            // Types of Items
            // 0 - Weapon
            // 1 - Food
            // 2 - Reg Item
            // 3 - Helmet
            // 4 - Armor Legs
            // 5 - Chest Armor
            // 6 - Shield

        public String[] getItem(int e) {

        String[] stats = new String[7];

            String name = "Null";
            String desc = "None";
            String typeOf = "0";
            String attackAdd = "0";
            String defenseAdd = "0";
            String canSell = "true";
            String canEat = "false";
            String earnedCoins = "0";



            if (e == 1) {

        name = "Pickaxe";
        desc = "Can be used to mine with.";
        typeOf = "2";
        attackAdd = "2";
        earnedCoins = "5";
        }

      return new String[] { name, desc, typeOf, attackAdd, defenseAdd, canSell, canEat, earnedCoins};

    }

}

/ p>

Thanks.

推荐答案

您不能从静态方法调用非静态方法。所以要么使 addInv static:

You can't call a non static method from a static method. So either make addInv static:

public class Item {
    ...

    public static void main(String args[]) {
        addInv(1);
    }

    public static void addInv(int e) {
        ...
    }

    public static String[] getItem(int e) {
        ...
    }
}

使所有这些方法静态可能不是一个合适的设计,您可能更喜欢在 main :中使用 Item / p>

But making all these methods static may not be an appropriate design, you might prefer using an Item instance in your main:

public class Item {
    ...

    public static void main(String args[]) {
        Item item = new Item();
        item.addInv(1);
    }

}

顺便说一句,




  • Code Conventions for the Java Programming Language
  • How to Write Doc Comments for the Javadoc Tool

这篇关于Java非静态方法addInv(int)不能从静态上下文引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-21 03:16