简体   繁体   English

Java-如何为子类的子类调用复制构造函数?

[英]Java - how do I call copy constructor for subclass of subclass?

So I have class called 'EducationalBook' that is subclass of class 'Book'. 因此,我有一个名为“ EducationalBook”的类,它是“ Book”类的子类。 And class 'Book' is the subclass of class 'PaperPublication'. 而“书籍”类是“纸张出版”类的子类。

In my understanding, copy constructor for normal class(like 'PaperPublication') can be written as follow: 以我的理解,普通类(如“ PaperPublication”)的复制构造函数可以编写如下:

public class PaperPublication
{
    String title;
    double price;
    int nOP; //number of pages
    public PaperPublication(PaperPublication p)
    {
        title = p.title;
        price = p.price;
        nOP = p.nOP;
    }...

However, I am not sure how to build a copy constructor for a subclass with parameter of that class itself only(for example: 'Book' class with parameter (Book b) ONLY). 但是,我不确定如何仅为具有该类本身参数的子类构建复制构造函数(例如:仅具有参数(Book b)的'Book'类)。 The reason why I want only one parameter is because there's a rule that for copy constructor of subclass, I have to call parent class's copy constructor(I believe this means use "super()" right?). 我只需要一个参数的原因是因为有一个规则,对于子类的副本构造函数,我必须调用父类的副本构造函数(我相信这意味着使用“ super()”对吗?)。 After some research, though I did not find a clear answer, I built as such: 经过研究,尽管我没有找到明确的答案,但我还是这样构建的:

public class Book extends PaperPublication
{
    long ISBN;
    int issued;
    String author;
    public Book(Book b)
    {
        super(b.getTitle(), b.getPrice(), b.getNOP());
        ISBN = b.ISBN;
        issued = b.issued;
        author = b.author;
    }...

I am not even sure this is correct way to do it, but it at least compiled without occurring any compile-stage error. 我什至不知道这是正确的方法,但是至少在编译时没有发生任何编译阶段错误。

However, for EducationalBook class, I don't have any single idea to build a copy constructor. 但是,对于EducationalBook类,我没有一个构想来构建副本构造函数。 Since Java does not allow something like super.super(), and I am not allowed to utilize parametrized constructor, but only copy constructor of parent class. 由于Java不允许使用诸如super.super()之类的东西,并且不允许使用参数化的构造函数,因此只能使用父类的复制构造函数。

public class EducationalBook extends Book
{
    int edition;
    String speciality;
    public EducationalBook(EducationalBook e)
    {
        //??? no clue..
    }
    public EducationalBook() // default constructor. is this right?
    {super();}
}

(And for additional question, is that default constructor right?) (还有其他问题,默认构造函数对吗?)

I feel like my understanding of inheritance of Java is weak overall. 我觉得我对Java继承的整体了解很薄。 Any enlightenment for me? 对我有什么启发?

It's easier than you think. 它比您想象的要容易。

The copy constructor of Book : Book的副本构造函数:

public Book(Book b)
{
  super(b);
  ISBN = b.ISBN;
  issued = b.issued;
  author = b.author;
}

And the copy constructor of EducationalBook : 以及EducationalBook的副本构造函数:

public EducationalBook(EducationalBook b)
{
  super(b);
  edition = b.edition;
  speciality = b.speciality;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM