2016-08-29 77 views
1

所以,我是面向對象編程的新手。我做的下一excercises:從Book-class獲取作者名簡單

  1. 鑑於定義爲具有以下屬性的一類書:

    Author author; 
    String title; 
    int noOfPages; 
    boolean fiction; 
    

    寫的每個屬性的標準get/set方法頭。

  2. [編碼]其實代碼和編譯基於鍛鍊methodscalled的屬性和get/set Book類1.

這是我的代碼:

public class Author { 

    //private variable 
    private String name; 
    private String gender; 
    //constructor 
    public Author (String name, String gender){ 
     this.name = name; 
     this.gender = gender; 
    } 
    //getters 
    public String getName(){ 
     return name; 
    } 
    public String getGender(){ 
     return gender; 
    } 
public class Book { 

    //private variables 
    private Author author; 
    private String title; 
    private int noOfPages; 
    private boolean fiction; 

    //constructor 
    public Book(String title, int noOfPages, boolean fiction){ 
     this.author=new Author ("Jacquie Barker","Female"); 
     this.title = title; 
     this.noOfPages=noOfPages; 
     this.fiction = fiction;  
    } 

    //getters 
    public Author getAuthorsName(){ 
     return this.author; 
    } 
    public String getTitle(){ 
     return title; 
    } 

    public int getNoOfPages(){ 
     return noOfPages; 
    } 

    public boolean getFiction(){ 
     return fiction; 
    } 

    //setters 
    public void setAuthor(Author newAuthor){ 
     author=newAuthor; 
    } 
    public void setTitle (String title){ 
     this.title=title; 
    } 
    public void setNoOfPages(int noOfpages){ 
     this.noOfPages=noOfpages; 
    } 
    public void setfiction(boolean fiction){ 
     this.fiction=false; 
    } 

    public String toString(){ 
     return "Title: " + this.title + "\n"+"Author: " + this.author + "\n" + 
       "No. of pages: " + this.noOfPages + "\n" + "Fiction: " + this.fiction; 
    } 
} 

這裏主要摘錄:

Title: Beginning in Java Objects 

Author: [email protected] 

No. of pages: 300 

Fiction: true 

由於喲你可以看到,該程序不打印作者的名字。

我欣賞所有幫助!

+1

你期望它打印什麼?爲什麼? – shmosel

+1

重寫'toString'方法。當你將'Author'與'String'連接起來時,它被隱式調用。 – RamenChef

回答

0

如果你想返回作者的名字,將其更改爲:

public Author getAuthorsName(){ 
    return this.author; 
} 

到:

public String getAuthorsName(){ 
    return this.author.getName(); 
} 

目前您從方法返回Author類的對象,所以得到作者姓名,您需要在調用代碼中調用author.getName()或更改現有方法以返回作者姓名,如上所述。

+0

非常感謝您的快速和有益的迴應 – Nami

1

您應該在Author類中實現toString。

public class Author { 

     //private variable 
     private String name; 
     private String gender; 
     //constructor 
     public Author (String name, String gender){ 
      this.name = name; 
      this.gender = gender; 
     } 
     ... 
     public String toString() { 
      return "Name " + name + "\t Gender: " + gender + "\n"; //Somethign like this. 
     } 
} 
+0

非常感謝您的幫助 – Nami

2

您有兩種選擇。首先,你可以簡單地重構代碼在你Book.toString()方法打印筆者的實際名稱:

public String toString(){ 
    return "Title: " + this.title + "\n"+"Author: " + this.author.getName() + "\n" + 
      "No. of pages: " + this.noOfPages + "\n" + "Fiction: " + 
} 

其次,你可以覆蓋toString()方法在Author類返回作者的名字。然後,你可以留下你的Book.toString()方法,因爲它是,因爲當你嘗試打印Author物體Java將調用此toString()方法:

public class Author { 
    // print the author's name when an Author object appears in a print statement 
    public String toString() { 
     return this.name; 
    } 
} 

,然後爲你:

public class Book { 
    public String toString(){ 
     return "Title: " + this.title + "\n"+"Author: " + this.author + "\n" + 
      "No. of pages: " + this.noOfPages + "\n" + "Fiction: " + this.fiction; 
    } 
} 
+0

非常感謝您對我的問題給予的非常有幫助和詳細的回覆! – Nami

1

如果您正在使用像Eclipse,IntelliJ或NetBeans這樣的IDE,您可以讓它們生成這些標準的getter和setter。探索菜單。

這樣,就不會出現拼寫錯誤(如setfiction,f應該是大寫)。

由於您是初學者,您應該先自己寫,然後讓它們自動生成,以便您可以比較您的解決方案。