首页 > 解决方案 > 如何将实例化对象用于使用数组的方法的参数

问题描述

即使我正确实例化了我的对象,我也发现很难使用我的方法。关于我哪里出错的任何想法?

示例:我尝试编译 java 文件,但我得到的错误是

“不兼容的类型:字符串无法转换为书籍”

我认为问题是我的实例化对象被强制转换为字符串,但问题是我使用正确的语法来调用字符串。但是,它仍然不会将其读取为字符串,并表示实例化的对象无法转换为“Books”类。

我已经搜索过它,但他们只说该对象尚未创建。但是,我检查了我的代码,甚至在将它放入方法参数之前就已经实例化了我的对象。

我什至尝试使用特定特征自行打印对象,结果很好。所以我想它会一直上升,直到它被放入一个方法中。

我不明白的一件事是我需要将该对象引用到方法中。

这是我的代码:


class Books{
    String type;
    int pages;
    Books[] booklist;
    int bookcounter = 0;

    //Constructor to initialize the object "book"
    Books(int input){
        if(input == 1){
            this.type = "Math";
            this.pages = 5;
        }
        if(input == 2){
            this.type = "Physics";
            this.pages = 9;
        }
        if(input == 3){
            this.type = "Economics";
            this.pages = 20;

        }
    }

    //This method needs to add the instantiated object to the array list
    void addbooktype(Books kind){

    System.out.println("You chose: " + kind);
    System.out.println("Adding to the list...");        

    booklist[bookcounter++] = kind;
    }

    void printbooks(){
for(int i = 0; i <= bookcounter; i++){
    int y = i+1;
    System.out.println("Book #"+ y + "is: " +this.booklist[i].type);
    System.out.println("With pages of: " + this.booklist[i].pages);
                }
    }

public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    int bookchoice;
    int choice;
    String booktype;
    int booknum = 0;


    do{
        System.out.println("===========Menu===========");
        System.out.println("[1] - Pick a book \n[2] - Print book list\n[0] - Exit");
        System.out.println("==========================");
        System.out.print("Choice: ");
        choice = sc.nextInt();

        switch(choice){
            //Selects and adds a book to the list
            case 1:
                System.out.println("Choose your book: ");
                bookchoice = sc.nextInt();

                Books book = new Books(bookchoice);
                System.out.println(book.type);

                booktype = book.type;

                book.addbooktype(booktype);

                booknum++;

                break;


            //Prints the book list
            case 2:
            System.out.println("List of Books: ");

            book.printbooks();
                break;

            case 0:
            System.out.println("Exit");
                return;

            default: System.out.println("Input not found.");

    }
    }while(choice!=0);

}
}

我得到的错误是关于“book.addbooktype(booktype);”

这就是它困扰我的地方,我打印了反对意见,甚至将其放入 String 容器中,但它仍然拒绝它。我不知道我哪里错了。当它进入方法时,它不会读取参数。有什么想法吗?

标签: javaarraysclassconstructorinstantiation

解决方案


您的方法 addbooktype 需要一个 Books 类型的参数,而您需要一个 String 参数

void addbooktype(书籍种类){

如果您进行此细微更改,您的代码将起作用:

void addbooktype(字符串种类){

编辑:根据评论,我似乎误解了代码。话虽如此,这就是你可以做的:

代替

book.addbooktype(booktype);

book.addbooktype();

并更换

void addbooktype(Books kind){

    System.out.println("You chose: " + kind);
    System.out.println("Adding to the list...");        

    booklist[bookcounter++] = kind;
}

void addbooktype(){

    System.out.println("You chose: " + this.kind);
    System.out.println("Adding to the list...");        

    booklist[bookcounter++] = this;
}

这会将当前调用的对象添加到您的数组中,并允许您稍后使用它。


推荐阅读