2016-08-29 41 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应该是大写)。

由于您是初学者,您应该先自己写,然后让它们自动生成,以便您可以比较您的解决方案。