【问题标题】:Get Author name from composite class Book从复合类 Book 中获取作者姓名
【发布时间】:2020-01-20 15:41:50
【问题描述】:

所以,我是面向对象编程的新手。我已经编写了一个代码来连接复合类和组件类,我需要包含至少一个允许复合类与组件类通信以计算一些值的方法。但是我的Book 类没有显示作者的名字。

这是我的代码:

public class Author{

private String name;
private String yearOfBirth;  

    public Author ()
    this.name = null;
    this.yearOfBirth = null;   
    }

    public Author (String aName)
    {
       this.name = aName;
       this.yearOfBirth = null;

   }
    public Author(String aName, String     aYear)
    {
  this.name = aName;
  this.yearOfBirth = aYear;
  }

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

 public void setYearOfBirth(String aYear)
  {
  this.yearOfBirth = aYear;
 }

    public String getName()
    {
     return this.name;
     }

    public String getYearOfBirth()
     {
      return this.yearOfBirth;
    }  

public String toString()
{
   return this.name + "(Born " + this.yearOfBirth + ")";
}

图书课

public class Book
{

 private String title;
 private String yearPublished;
 private Author author;


 public Book(String aTitle, String aYear,Author theAuthor)
  {
    this.title = aTitle;
    this.yearPublished = aYear;
    this.author = theAuthor;
   }


 public Book(String aTitle)
 {
   this.title = aTitle;
   this.yearPublished = null;
   this.author = new Author();
 }

 public void setAuthorName(String aName)
  {
      this.author.setName(aName);
 } 

  public void setYearPublished(String aYear)
  {
     this.yearPublished = aYear;
    }

     public String getTitle()
  {
     return this.title;   
  }

  public String getYearPublished()
   {
    if (this.yearPublished == null)
    {
       return "Unknown";
     }

     return this.yearPublished;
      }

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

    public boolean isBorn()
    {
         return(Integer.parseInt
   (this.author.getYearOfBirth()) < 1900);
  }

public String toString()
  {

 {
    return "Title: " + this.title + ", Author: " + this.author.getName() + ", yearPublished: " + this.yearPublished+ ".";          
 } 

因此,当我尝试通过执行以下操作获取作者姓名时:Book hp = new Book ("Harry Potter","JK Rowling","2000"); 它会在显示窗格中显示

Compilation failed (20/01/2020 15:02:12)
Error: line 1 - no suitable constructor found for Book(java.lang.String,java.lang.String,java.lang.String)

如您所见,程序不会打印作者的姓名。

感谢所有帮助!

【问题讨论】:

  • 这与您的 getAuthorName 方法无关 - 您正在使用错误的参数调用 Book 构造函数,如错误消息所示。
  • 请不要从您的问题中删除重要信息。它不仅适合您,也适合未来的读者。如果您删除代码,他们将没有关于正在发生的事情的上下文。

标签: java composite composite-component


【解决方案1】:

因为你的建设者的电话不好。

图书课正在等待获取 像 String 这样的标题, 一年的String类型

和一个作者类型的作者

例如Book book =new Book("test", "2020", new Author("MyAuthor"));

【讨论】:

  • 唯一的问题是,当我检查它时.. 作者的值是“检查对象”而不是作者的名字。标题的值是正确的“哈利波特”和年份“2020”
  • 请问您如何检查您的代码? “检查对象”意味着它是一个对象,而不仅仅是一个值。您可以调用 toString() 方法来获取详细信息。示例:Book book =new Book("test", "2020", new Author("MyAuthor")); System.out.println(book.toString());
【解决方案2】:

book的构造函数接受的第三个参数是作者对象。

所以调用应该是这样的

Book hp = new Book ("Harry Potter",2000,new Author("JK Rowling",1965));

【讨论】:

  • 另外我将如何测试我的 isBorn 方法?如下所示,找不到方法 jkr.isBorn();
【解决方案3】:

正如错误消息告诉你的那样,你没有为Book定义一个接受三个字符串的构造函数,因为你试图在

中调用它
Book hp = new Book ("Harry Potter","JK Rowling","2000");

(作为旁注,您有作者和年份)

这里有两个选择。

首先,可以在调用Book构造函数之前创建作者

Author jkr = new Author("JK Rowling");
Book hp = new Book("Harry Potter", jkr, "2000");

或者您可以在Book 中创建一个新的构造函数,该构造函数期望作者的姓名为字符串,自己构建作者对象,然后调用将其设置为新书的作者

public Book(String title, String author, String year) {
    this.title = title;
    this.author = new Author(author);
    this.yearPublished = year;
}

第一个版本的优点是你可以充分利用你的Author类,例如设置作者的出生年份

Author jkr = new Author("JK Rowling", "1965");
Book hp = new Book("Harry Potter", jkr, "2000");

第二个版本不允许您在不创建另一个接受标题、作者姓名、作者出生年份、书籍出版年份的构造函数的情况下执行此操作。

【讨论】:

  • 所以这工作 Book hp =new Book("Harry Potter", "2020", new Author("JK Rowling"));但是当我检查它时,作者的值是“检查对象”而不是作者的名字。当我双击它时,它会显示作者姓名和出生日期
  • 应该是这样的。 Author 是一个类,而你书中的author 是该类的一个实例,所以它不能只是一个字符串。您必须检查对象并查看其字段的值。如果我刚才说的不清楚,我怀疑你应该退后一步,重新阅读你用来学习这个的任何书籍/教程的部分。
  • 那就没问题了。谢谢您的帮助。就是这么简单!我在挣扎。另外我将如何测试我的 isBorn 方法?如下所示,找不到方法 jkr.isBorn();
  • isBornBook 的一部分,而不是作者的一部分,如果你问我,这没有多大意义。将其移入Author(并将this.author.getYearOfBirth() 修改为this.getYearOfBirth)。请注意,与现在一样,该方法仅检查作者是否出生于 1900 年之前。我不知道这对您有多大用处,但仍然如此。
  • 在相关说明中,Book::yearPublishedAuthor::birthDate 可能都应该是 ints,而不是字符串。当然,您必须更新其 getter、setter 和构造函数的方法签名。
猜你喜欢
  • 2017-11-25
  • 2021-12-17
  • 2017-05-10
  • 1970-01-01
  • 2011-02-25
  • 2018-01-13
  • 1970-01-01
  • 2017-08-09
  • 1970-01-01
相关资源
最近更新 更多