2015-01-09 132 views
0

我的程序應該返回兩個不同對象的不同屬性。在我的主要方法中,我在創建新對象時將這些屬性設置爲參數。但是當我調用那些getter方法(我已經寫在一個單獨的類中,如果需要的話我可以發佈那個類),它不會返回所有的屬性。它只打印出第一個屬性(也被設置爲第一個參數),而不是其他兩個值。我不知道我做錯了什麼。爲什麼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()); 

    } 

} 

Book類:

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

} 
+1

提供設計Book類也是如此。 –

+1

你可以發佈書類,你得到的實際輸出和你期望的輸出嗎? – Paolo

+0

Luiggi門多薩,保羅,我現在編輯。 – Riyana

回答

2

您需要遍歷數組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; 
} 
+2

或使用'Arrays.toString(book1.getAuthors()'。 –

0

當你想打印您可以使用此字符串數組。

System.out.println(Arrays.toString(book1.getAuthors())); 
0
  1. 沒有與書構造一個問題:我想你需要定義如何getAuthors

    public Book(String name, String isbn, String[] authors) { 
        this.name = name; 
        this.isbn = name; // you are setting isbn as name! 
        this.authors = authors; 
    } 
    
  2. 打印作者陣列,像What's the simplest way to print a Java array?

相關問題