从复合 class 书中获取作者姓名
Get Author name from composite class Book
所以,我是面向对象编程的新手。我已经编写了连接复合体和组件 class 的代码,我需要至少包含一个允许复合体 class 与组件 class 通信的方法,以便计算一些值.但是我的 Book
class 没有显示作者的名字。
这是我的代码:
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 + ")";
}
书籍CLASS
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)
如您所见,程序没有打印作者姓名。
感谢大家的帮助!
book的构造函数接受的第三个参数是author的对象。
所以调用应该像
Book hp = new Book ("Harry Potter",2000,new Author("JK Rowling",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
中创建一个新的构造函数,它期望作者的名字是一个字符串,构建作者 object 本身,然后调用将其设置为新书的作者
public Book(String title, String author, String year) {
this.title = title;
this.author = new Author(author);
this.yearPublished = year;
}
第一个版本的优点是你可以充分利用你的Author
class,例如设置作者的出生年份
Author jkr = new Author("JK Rowling", "1965");
Book hp = new Book("Harry Potter", jkr, "2000");
如果不创建另一个接受标题、作者姓名、作者出生年份、书籍出版年份的构造函数,第二个版本不允许您这样做。
因为你builder的电话不好
图书class正在等待获取
像 String 这样的标题,
String 类型的一年
和作者类型作者
示例Book book =new Book("test", "2020", new Author("MyAuthor"));
所以,我是面向对象编程的新手。我已经编写了连接复合体和组件 class 的代码,我需要至少包含一个允许复合体 class 与组件 class 通信的方法,以便计算一些值.但是我的 Book
class 没有显示作者的名字。
这是我的代码:
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 + ")";
}
书籍CLASS
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)
如您所见,程序没有打印作者姓名。
感谢大家的帮助!
book的构造函数接受的第三个参数是author的对象。
所以调用应该像
Book hp = new Book ("Harry Potter",2000,new Author("JK Rowling",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
中创建一个新的构造函数,它期望作者的名字是一个字符串,构建作者 object 本身,然后调用将其设置为新书的作者
public Book(String title, String author, String year) {
this.title = title;
this.author = new Author(author);
this.yearPublished = year;
}
第一个版本的优点是你可以充分利用你的Author
class,例如设置作者的出生年份
Author jkr = new Author("JK Rowling", "1965");
Book hp = new Book("Harry Potter", jkr, "2000");
如果不创建另一个接受标题、作者姓名、作者出生年份、书籍出版年份的构造函数,第二个版本不允许您这样做。
因为你builder的电话不好
图书class正在等待获取 像 String 这样的标题, String 类型的一年
和作者类型作者
示例Book book =new Book("test", "2020", new Author("MyAuthor"));