2016-03-08 95 views
1

无法在Book类中创建作者对象。这确实是家庭作业,我自己想出了所有的方法,并且现在一直盯着这个作业2个小时。任何提示提示将不胜感激。我相信我只有3个参数的作者构造函数,否则我只是做了一个没有参数的作者构造函数,问题就会消失。java在子类中创建对象

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

感谢编辑 – javaNewb

回答

0

似乎很奇怪Book extends Author。 A Book不是Author

我想你想要做的就是创建一个具有Author一个Book对象是什么,但你在Author已经传递到你的Book构造,所以有什么问题?

class Book { 
    public Book(String title, Author author) { 
     this.title = title; 
     this.author = author; 
    } 
} 

如果你想知道如何创建Author,只需创建它,你将它传递给Book之前。

Author author = new Author("bob", "[email protected]", 'm'); 
Book book = new Book("some title", author); 

这有道理吗?

+0

感谢,多数民众赞成我感到困惑! – javaNewb

2

它应该是this.author = author;没有new关键字。

您正在构造函数中分配Author对象,而不是创建新对象。

顺便说一句,Book继承自Author,这意味着它已具有Author的功能。您不必将其保存为成员。

0

你继承在BookAuthor,继承is a relationship,意味着Book is a Author这是不是在现实世界中的场景真实的,你需要has a relation这是有道理的,即Book has a Author

所以,不要在书中扩展作者,保留作者的领域,因为你已经做了。改变你的设计。

0

有你的代码中的多个错误:

  1. 为@Guy说,你需要给笔者与this.author = author分配,而不new关键字。
  2. 通过在Book类中还包含一个具有相同名称的变量,您可以隐藏Author类的名称变量。从Book类中删除该变量以及getter和setter方法。
  3. 您没有无参数构造函数,因此您必须调用super(name, email, gender);来调用Book类中的父构造函数。
0

你应该用作者替换新作者,当你调用子类型的构造函数时,默认调用父类的默认构造函数。所以你需要在你的父类中指定一个默认构造函数,或者为了防止它在子类构造函数中显式调用父类的参数构造函数。

0

除了更改为this.author = author。 您还需要将super(name, name, 'M');添加到Book类的两个构造函数中。

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() 
{ 

} 
+0

能够做到这一点,谢谢你们 – javaNewb