简体   繁体   English

无法静态引用非静态方法库

[英]Cannot make a static reference to the non-static method Library

my professor refuses to tell me what I'm doing wrong and I for the life of my can't figure it out. 我的教授拒绝告诉我我在做什么错,而我一生都无法解决。 I'm just starting java so I may look foolish but 我刚开始使用Java,所以看起来可能很愚蠢,但是

import java.util.Scanner;

public class Library {
    MediaItem[] items = new MediaItem[100];
    int numberOfItems = 0;
    static int displayMenu(){
        @SuppressWarnings("resource")
        Scanner dog = new Scanner(System.in);
        System.out.println("Menu:\n1:Add New Item\n2:Mark Item as Loaned\n3:List All Items\n4:Mark Item as Returned\n5:Quit");
    int cat = dog.nextInt();
    return cat;
}
void addNewItem(String title, String format){
    MediaItem item = new MediaItem();
    item.setTitle(title);
    item.setFormat(format);
    items[numberOfItems] = item;
    numberOfItems++;
}
void markItemOnLoan(String title, String name, String date){
    for (int i=0;i<numberOfItems;i++){
        MediaItem item = items[i];
        String title5 = item.getTitle();
        if (title5 == title){
            item.markOnLoan(name, date);
            i=numberOfItems;
        }

        }
}
String[] listAllItems(){
    String[] all = new String[numberOfItems];
    for (int i=0;i<numberOfItems;i++){
        MediaItem item = items[i];
        if (item.onLoan=true){
            all[i]=item.getTitle()+" "+item.getFormat()+" "+item.getOnLoan()+" "+item.getLoanedTo()+" "+item.getDateLoaned()+" ";
        }else{
            all[i]=item.getTitle()+" "+item.getFormat()+" "+item.getOnLoan()+" ";
        }

    }
    return all;
}
void markItemReturned(String title){
    for (int i=0;i<numberOfItems;i++){
        MediaItem item = items[i];
        String title1 = item.getTitle();
        if (i==numberOfItems){
            System.out.println("Title not found.");
        }else if (title1 == title){
            item.markReturned();
            i=numberOfItems;
        }
        }
}

public static void main(String[] args) {
    Library banana = new Library();
    String invalid = "Invalid Option";
    @SuppressWarnings("resource")
    Scanner dog = new Scanner(System.in);
    for (int i=0; i==0;){
        int cat = displayMenu();
        if (cat<=0){
            System.out.println(invalid);
        }
        if (cat>=6){
            System.out.println(invalid);
        }
        switch (cat){
        case 1:
            System.out.println("What is the title of the item?");
            String title = dog.nextLine();
            System.out.println("What is the format of the item?");
            String format = dog.nextLine();
            banana.addNewItem(title, format);
            break;
        case 2:
            System.out.println("What is the title of the item?");
            String title1 = dog.nextLine();
            System.out.println("What is your name?");
            String name = dog.nextLine();
            System.out.println("What is the date?");
            String date = dog.nextLine();
            banana.markItemOnLoan(title1, name, date);
            break;
        case 3:
            String[] list = banana.listAllItems();
            System.out.println("List of all items:\n");
            for (int o=0;o<banana.numberOfItems;o++){
                System.out.println(list[o]);
            }
            break;
        case 4:
            System.out.println("What is the title you are returning?");
            String title2 = dog.nextLine();
            banana.markItemReturned(title2);
            break;
        case 5:
            i++;
            break;
        }
    }
}
}

I'm having this problem throughout my code but I figure if you guys can answer this one I can figure the rest out on my own. 我在整个代码中都遇到了这个问题,但是我想如果你们能回答这个问题,我可以自己解决其余的问题。 Edit: I'm getting further, now I just need to figure out why it isn't reading the name and date. 编辑:我走得更远,现在我只需要弄清楚为什么它没有读取名称和日期。

In your switch statement, you call addNewItem, which is not declared static. 在您的switch语句中,您调用addNewItem,它不是被声明为静态的。

In order to call addNewItem, you must have an Object to call it from, unless you declare it static, or call obj.addNewItem() . 为了调用addNewItem,您必须有一个Object可以从中调用它,除非您将其声明为静态,或调用obj.addNewItem() You didn't include much code, so I'm not sure what class the object should be. 您没有包含太多代码,所以我不确定对象应该是什么类。

static void addNewItem(String title, String format){
    MediaItem item = new MediaItem();
    item.setTitle(title);
    item.setFormat(format);
    items[numberOfItems] = item;
    numberOfItems++;
}

the static keyword means that there is only a single instance of the method, field, or class even if there isn't an instance of the class associated with it. static关键字表示方法,字段或类只有一个实例,即使没有与之关联的类的实例也是如此。 Without the static modifier, there needs to be an instance of the class to access the method, field, or class from. 如果没有static修饰符,则需要一个类的实例来访问方法,字段或类的来源。

For a good example, see: https://stackoverflow.com/a/15828090/1732480 有关示例,请参阅: https : //stackoverflow.com/a/15828090/1732480

Edit: After you posted more code, you have no instance of Library to call addNewItem on. 编辑:发布更多代码后,您没有Library实例可以调用addNewItem。 Create a Library with Library l = new Library(); 使用Library l = new Library();创建一个库Library l = new Library(); and call l.addNewItem() 并调用l.addNewItem()

public static void main(String[] args) {
    Library l = new Library();
    String invalid = "Invalid Option";
    Scanner dog = new Scanner(System.in);
    for (int i=0; i==0;){
        int cat = displayMenu();
        if (cat<=0){
            System.out.println(invalid);
        }
        if (cat>=6){
            System.out.println(invalid);
        }
        switch (cat){
            case 1:
                System.out.println("What is the title of the item?");
                String title = dog.nextLine();
                System.out.println("What is the format of the item?");
                String format = dog.nextLine();
                l.addNewItem(title, format);
                break;
         ....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM