簡體   English   中英

為什么getter方法不返回任何對象的屬性?

[英]Why getter method doesn't return any object's attribute?

我的程序應為兩個不同的對象返回不同的屬性。 在我的主要方法中,我在創建新對象時將這些屬性設置為參數。 但是,當我調用這些getter方法(我已經在一個單獨的類中編寫了該方法)時,可以根據需要發布該類),但它不會返回所有屬性。 它僅打印出第一個屬性(也設置為第一個參數),而不打印其他兩個值。 我不知道我做錯了什么。

我的代碼:主類:

    package main;

public class Main {

    public static void main(String[] args) {

        //creating object for book 1 
        Book book1 = new Book("The brief history of time", "111", new String[]{"S. hawking", "hawking's friends"});
        //creating object for book 2
        Book book2 = new Book("100 years of solitude", "222", new String[]{"G.marquez", "marquez's friend"});

        System.out.println("All info for the first book: \n");

        System.out.println("Name: " + book1.getName());
        System.out.println("ISBN: " + book1.getIsbn());
        System.out.println("Authors: " + book1.getAuthors());

        System.out.println("\n\n");
        System.out.println("All info for the second book: \n");
        System.out.println("Name: " + book2.getName());
        System.out.println("ISBN: " + book2.getIsbn());
        System.out.println("Authors: " + book2.getAuthors());

    }

}

書本類:

    package main;

public class Book {
    //variables

    private String name;
    private String isbn;
    private String[] authors;

    //constructors
    public Book(String name, String isbn, String[] authors) {
        this.name = name;
        this.isbn = name;
        this.authors = authors;

    }

    //setters
    public void setName(String name) {
        this.name = name;
    }

    public void setIsbn(String isbn) {
        this.isbn = isbn;
    }

    public void setAuthors(String[] authors) {
        this.authors = authors;
    }

    //getters
    public String getName() {
        return name;
    }

    public String getIsbn() {
        return isbn;
    }

    public String[] getAuthors() {
        return authors;
    }

}

您需要迭代authors數組才能在其中打印字符串。 像這樣的東西:

    System.out.println("All info for the first book: \n");

    System.out.println("Name: " + book1.getName());
    System.out.println("ISBN: " + book1.getIsbn());
    for (String author : book1.getAuthors()) {
        System.out.println("Author: " + author);
    }

另外,您的Book類構造函數中存在一個問題:

public Book(String name, String isbn, String[] authors) {
    this.name = name;
    this.isbn = name; // this.isbn is not name!
    this.authors = authors;
}

一定是:

public Book(String name, String isbn, String[] authors) {
    this.name = name;
    this.isbn = isbn;
    this.authors = authors;
}

當您要打印字符串數組時,可以使用this。

System.out.println(Arrays.toString(book1.getAuthors()));
  1. Book構造函數存在問題:

     public Book(String name, String isbn, String[] authors) { this.name = name; this.isbn = name; // you are setting isbn as name! this.authors = authors; } 
  2. 我認為您需要定義getAuthors如何打印authors數組,例如打印Java數組的最簡單方法什么?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM