在Book类中创建一个作者对象时遇到了麻烦。这确实是Homework,我自己想出了所有的方法,现在已经盯着这个作业2个小时了。任何提示提示将不胜感激。我相信我只允许这个带有3个参数的Author构造函数,否则我只会创建一个没有参数的Author构造函数,问题就会消失。
public class Author {
protected String name;
protected String email;
protected char gender;
public Author(String name, String email, char gender)
{
this.name = name;
this.email = email;
this.gender = gender;
}
public String getName()
{
return name;
}
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
public char getGener()
{
return gender;
}
public String toString()
{
return ( name + "(" + gender + ")@" + email);
}
}
public class Book extends Author{
private String name;
private Author author;
private double price;
private int qtyInStock = 0;
public Book(String name, Author author,Double price)
{
this.author = new author;
this.name = name;
this.price = price;
}
public Book(String name, Author author, double price, int qtyInStock)
{
this.name = name;
this.author = author;
this.price = price;
this.qtyInStock = qtyInStock;
}
public String getName()
{
return name;
}
public Author getAuthor()
{
return author;
}
public double getPrice()
{
return price;
}
public void setPrice(double price)
{
this.price = price;
}
public int getQtyInStock()
{
return qtyInStock;
}
public void setQtyInStock(int qtyInStock)
{
this.qtyInStock = qtyInStock;
}
public String toString()
{
return (name + " by " + author + "(" + super.gender + ")at" + super.email);
}
}
答案 0 :(得分:2)
如果没有this.author = author;
关键字,则应为new
。
您正在构造函数中分配Author
对象,而不是创建新对象。
顺便说一句,Book
继承自Author
,这意味着它已具有Author
功能。您不必将其保存为成员。
答案 1 :(得分:0)
Book extends Author
似乎很奇怪。 Book
不是Author
。
我认为您要做的是创建一个Book
对象,其中Author
,但您已经将Author
传递给Book
构造函数,所以有什么问题?
class Book {
public Book(String title, Author author) {
this.title = title;
this.author = author;
}
}
如果您想知道如何创建Author
,只需在将其传递给Book
之前创建它。
Author author = new Author("bob", "bob@email.com", 'm');
Book book = new Book("some title", author);
这有意义吗?
答案 2 :(得分:0)
您在Author
继承Book
,继承为is a relationship
,意味着Book is a Author
在现实世界的情况下不正确,您需要has a relation
感觉,Book has a Author
。
所以不要在Book中扩展作者,保留一个作者的字段,就像你已经做过的那样。改变你的设计。
答案 3 :(得分:0)
您的代码中存在多个错误:
this.author = author
,而不是new
关键字。Author
类中包含同名变量来隐藏Book
类中的name变量。从Book
类中删除该变量以及getter和setter方法。Book
来调用super(name, email, gender);
类中的父构造函数。答案 4 :(得分:0)
您应该使用author替换新作者,同样在调用子类型的构造函数时,默认情况下会调用Parent类的默认构造函数。所以你需要在父类中指定一个默认构造函数,或者为了防止它在子类构造函数中显式调用父类的参数构造函数。
答案 5 :(得分:0)
除了改为this.author = author
。
您还需要将super(name, name, 'M');
添加到Book
类的两个构造函数中。
答案 6 :(得分:0)
public class Book extends Author{
private String name;
//private final String email;
// private char gender;
private Author author;// = new Author(name,super.email,super.gender);
private double price;
private int qtyInStock = 0;
public Book(String name, Author author,double price)
{
//super();
this.author = author;// new Author(name,super.email,super.gender);
super.name = author.getName();
super.gender = author.getGender();
super.email = author.getEmail();
this.name = name;
this.price = price;
}
public Book(String name, Author author, double price, int qtyInStock)
{
this.author = author;
super.name = author.getName();
super.gender = author.getGender();
super.email = author.getEmail();
this.name = name;
this.price = price;
this.qtyInStock = qtyInStock;
}
public String getName()
{
return name;
}
public Author getAuthor()
{
return author;
}
public double getPrice()
{
return price;
}
public void setPrice(double price)
{
this.price = price;
}
public int getQtyInStock()
{
return qtyInStock;
}
public void setQtyInStock(int qtyInStock)
{
this.qtyInStock = qtyInStock;
}
public String toString()
{
return (super.name + " by " + this.name + "(" + super.gender + ")at" + super.email);
}
还将此添加到作者类
public Author()
{
}