【问题标题】:How to use an instantiated object into a paramater for the method with use of arrays如何将实例化对象用作使用数组的方法的参数
【发布时间】:2019-09-04 18:21:13
【问题描述】:

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

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

【问题讨论】:

  • 一个类应该代表一些东西。你的Book 类是代表一本书还是一本书的列表?
  • 我想要的是它代表一个书籍列表,这就是我包含“Books [] booklist”数组的原因。还是错了?
  • Book.Type 只是字符串。您需要传递对象本身,即 book 或更改方法以接受 String
  • 那么它不应该有typepages 字段,因为书籍列表没有“类型”,也没有“页面”。我认为您需要将此类重命名为 BookList 并将 typepages 移动到一个新类 Book 中。
  • 欢迎来到 StackOverflow。正如tour 中所述,此站点是有用问题及其答案的存储库,不是教程站点或帮助/讨论论坛。我担心您对一些基本概念(例如类、类型和实例)有一些根本性的误解,不幸的是,SO 并没有真正设置为教程“论坛”。请拨打tour,访问help center,尤其是阅读How to AskWhy is “Can someone help me?” not an actual question?,了解如何有效使用本网站。

标签: java arrays class constructor instantiation


【解决方案1】:

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

void addbooktype(书籍种类){

如果你做出这个微小的改变,你的代码就可以工作:

void addbooktype(String kind){

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

替换

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

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

【讨论】:

  • 但他正试图将其添加到Book 类型的数组中,这将如何解决他的问题?他真的只需要listsingle Book 的 2 个单独的类。
  • 成功了!!但是,我很困惑是不是我们应该放置类的引用而不是数据类型?这就是为什么我提出类“书籍”而不是“字符串”。我认为这个约定只适用于构造函数而不是方法。
  • 嘿,我以前的解决方案会奏效,但会在稍后阶段导致问题。编辑后尝试解决方案。我在该解决方案中使用类的引用。
  • 种类不能作为字段使用。我可以建议一个新的编辑吗?
【解决方案2】:

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

book.addbooktype(booktype);

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

传递书籍对象:

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 word this,而不是将对象作为多余的参数传递。

根据java文档Using the this Keyword

在实例方法或构造函数中,this 是对当前对象的引用——正在调用其方法或构造函数的对象。您可以使用 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;
}

当您必须比较对象时,传递对象和这很有用的一个示例。假设您有两个对象 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; 
}

【讨论】:

  • 我试过了,它一直工作到它添加到数组的那一行。它提出了“字符串”不能转换为书籍,但我认为该值已经在它只是拒绝将其添加到数组中的方法中。
  • @Zidane101 我已更新答案以包含您的问题。
  • @Zidane101 Idk 为什么删除了评论但 nullpointer 是因为您的数组 bookList 从未实例化,我也将在一段时间内更改我的答案,请检查一下。
  • 谢谢 我有点理解你的意思。我只是认为方法中应该有参数,这就是为什么我如此专注于放置一些而忘记了它可能没有参数的原因。谢谢!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多