2015-01-09 101 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?

相关问题