为什么由于来自静态上下文而无法调用或无法调用这些方法?

时间:2019-05-06 00:52:53

标签: java

因此,我在APCS作业中遇到有关调用方法的问题。我尝试从“列表”类调用的所有方法都给我错误“无法找到符号方法getList()”,而我的列表制作器类的所有方法都给我错误“非静态方法setObject(java.lang .String)/ setAmount(int)不能从静态上下文中引用。”我还有一个来自ListMaker类的方法,该方法给我错误:“类ListMaker中的方法addList无法应用于给定类型;必需:ListMaker,找到:java.lang.String,int,原因:实际长度与形式长度不同” 。关于我能做什么的任何想法?

主类:

public class groceryList
{
    public void main(String[] args)
    {
        Scanner keyboard = new Scanner(System.in);

        String object = "";
        int amount = 0;
        boolean x = true;

        ListMaker list = new ListMaker(object, amount);

        // Provides appropriate commands
        System.out.println("Please add items to your list along with the amount you would like");
        System.out.println("Please enter \"done\" if you are done entering items");
        System.out.println("Please enter \"remove\" if you would like to remove items from your list");
        System.out.println("Please enter \"print list\" if you would like to print your list");

        while(x == true)
        {
            object = keyboard.nextLine();

            if(object.toUpperCase().equals("DONE")) // Prints out final list and ends loop
            {
                x = false;
                System.out.println("Here is your final list");
                List.getList();
                break;
            } else if(object.toUpperCase().equals("REMOVE")) // Removes items from list
            {

            } else if(object.toUpperCase().equals("PRINT LIST")) // Prints the list
            {
                List.getList();
            } else // adds items to list
            {
                ListMaker.setObject(object);
                System.out.println("Please enter the amount of that item you would like");
                    amount = keyboard.nextInt();
                ListMaker.setAmount(amount);
                ListMaker.addList(object, amount);
                continue;
            }
        }
    }
}

ListMaker类:​​

public class ListMaker
{
    private String object = "";
    private int amount = 0;

    private ArrayList <ListMaker> groceries;

    public ListMaker(String o, int a)
    {
        object = o;
        amount = a;
    }

    ListMaker()
    {
        object = "";
        amount = 0;
    }

    public void setObject(String o)
    {
        this.object = o;
    }

    public void setAmount(int a)
    {
        this.amount = a;
    }

    public String getObject()
    {
        return object;
    }

    public int getAmount()
    {
        return amount;
    }

        public void addList(ListMaker l)
    {
        this.groceries.add(l);
    }

}

列表类别:

public class List
{
    private ArrayList <ListMaker> groceries;

    public List()
    {
        groceries = new ArrayList<ListMaker>();
    }

    public ArrayList getList()
    {
        return groceries;
    }

}

0 个答案:

没有答案