Java 从复合类书中获取作者姓名
所以,我是面向对象编程新手。我已经编写了连接复合类和组件类的代码,我需要至少包含一个方法,该方法允许复合类与组件类通信,以便计算一些值。但是我的Java 从复合类书中获取作者姓名,java,composite,composite-component,Java,Composite,Composite Component,所以,我是面向对象编程新手。我已经编写了连接复合类和组件类的代码,我需要至少包含一个方法,该方法允许复合类与组件类通信,以便计算一些值。但是我的书类没有显示作者的姓名 这是我的代码: public class Author{ private String name; private String yearOfBirth; public Author () this.name = null; this.yearOfBirth = null; }
书
类没有显示作者的姓名
这是我的代码:
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=新书(“哈利波特”,2000年,新作者(“JK罗琳”,1965年)) 正如错误消息告诉您的,您没有为
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
中创建一个新的构造函数,该构造函数期望作者的姓名作为字符串,构建author对象本身,然后调用并将其设置为新书的作者
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 Book=新书(“测试”,“2020”,新作者(“我的作者”)
这与您的getAuthorName
方法无关-如错误消息所示,您使用错误的参数调用Book
构造函数。请不要从问题中删除重要信息。这不仅仅是为了你,也是为了未来的读者。如果你删除了代码,他们就不会知道发生了什么。唯一的问题是,当我检查它时。。author的值是“inspect object”,而不是作者的名称。标题的值是正确的《哈利波特》和出版的《2020》我可以问一下你们是如何检查你们的代码的吗?“检查对象”意味着它是一个对象,而不仅仅是一个值。您可以调用toString()方法来获取详细信息。示例:Book Book=新书(“测试”,“2020”,新作者(“我的作者”);System.out.println(book.toString());所以这本书是hp=新书(“哈利波特”,“2020”,新作者(“JK罗琳”);但是当我检查它时,author值是“inspect object”,而不是Authors名称。当我双击它时,它确实显示了作者的名字,这就是它应该是什么样子Author
是一个类,而书中的Author
是该类的实例,因此它不能只是一个字符串。您必须检查对象并查看其字段的值。如果我刚才说的不清楚,我想你应该退后一步,重新阅读你用来学习这一点的任何书籍/教程的部分。谢谢你的帮助。太简单了!我在挣扎。我如何测试我的isBorn方法?执行以下操作时,表示找不到方法jkr.isBorn()isBorn
是这本书的一部分,不是作者的,如果你问我的话,这没什么意义。将其移动到作者
(并将this.Author.getYearOfBirth()
修改为this.getYearOfBirth
)。请注意,现在的方法只检查作者是否在1900年之前出生。我不知道这对你有多大帮助,但仍然是。相关的一点是,Book::yearPublished
和Author::birthDate
应该是int
s,而不是字符串。当然,您必须为它们的getter、setter和构造函数更新方法签名。还有,我将如何测试我的isBorn方法?执行以下操作时,表示找不到方法jkr.isBorn();
Author jkr = new Author("JK Rowling", "1965");
Book hp = new Book("Harry Potter", jkr, "2000");