如何使用实例化对象作为使用数组的方法的参数

How to use an instantiated object into a paramater for the method with use of arrays

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

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

"incompatible types: String cannot be converted to Books"

我认为问题是我的实例化对象被强制转换为字符串,但问题是我使用了正确的语法来调用字符串。但是,它仍然没有将其作为字符串读取,并表示无法将实例化对象转换为 "Books" class.

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

我什至尝试自己打印具有特定特征的对象,结果很好。所以我想它会一直持续到它被放入一个方法中。

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

这是我的代码:


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 容器中,但它仍然拒绝它。我不知道我哪里出错了。当它进入方法时,它不会读取参数。有什么想法吗?

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

void addbooktype(Books kind){

如果您稍作更改,您的代码就会正常工作:

void addbooktype(String kind){

编辑:根据评论,我似乎误解了代码。话虽如此,您可以执行以下操作:

替换

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;
}

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

问题是您的方法只接受 Class Book 的对象。但是,当您调用该函数时

book.addbooktype(booktype);

你给它的类型是 String(在你的书中 class 类型是 String 变量)。要解决这个问题,您需要传递书籍对象或更改方法本身

正在传递图书对象:

Books book = new Books(bookchoice);
book.addbooktype(book);

在函数中你可以做这样的事情

void addbooktype(Books book) {

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

    booklist[bookcounter++] = book;
}

(稍后添加)使用此:

这种方法也利用了对象,但它比上述方法要好。您可以使用 java 字 this,而不是将对象作为冗余参数传递。

根据 java 文档 Using the this Keyword

Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this.

所以当你调用函数时

book.addbooktype(book);
 ^   and          ^ are same
 |                | 

方法内部addbooktype

void addbooktype(Books book) {
   this and book would also be same. 
}

所以你可以这样做

book.addbooktype();

addbooktype 将是

void addbooktype() {

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

    booklist[bookcounter++] = this;
}

传递对象和 this 的一个例子是当你必须比较对象时。假设您有两个对象 book1 和 book2。你会做这样的事情

int compareResult = book1.compareTo(book2);

你的方法是

public int compareTo(Book book) {
   Now here this would be book1 
   and book would be book2

   Also keep in mind this.type would be same as type. type and this.type are both 
   referring to same object i.e who we called function on (book1).  
}

改变函数:

或者你可以像这样改变你的功能,尽管我建议你传递对象本身。如果你只是传递字符串,你将无法将它存储在 Books[] booklist

void addbooktype(String type) {
    System.out.println("You chose: " + type);
    System.out.println("Adding to the list...");        

    // The line below will give error unless you change Books[] booklist;
    // to  String[] booklist;
    booklist[bookcounter++] = kind; 
}