我正在通过一个计算机科学论文过去的考试,并在这里变得有点困惑。构造函数替换默认构造函数?
这些指令是编写一个类书(完成),它有两个数据字段(也完成)一个构造函数初始化两个值(完成以及)和一个构造函数替换默认构造函数(不知道这是什么是关于)。我已经研究过它,并通过了我的实验笔记,但我不明白他们在问什么。
下面的代码
public class Book{
//here's the two data fields
int pages;
String title;
public Book (int pageNum, String titleString){//here's the constructor to set the values
pages = pageNum;
title = titleString;
}
}
//so where's the other constructor that replaces the default constructor supposed to go?
该构造函数替换了默认的构造函数。如果类中没有其他构造函数,则默认构造函数才存在。请参阅[语言规格](https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.8.9)。 –
如果您希望人们尝试阅读,则应该正确缩进代码。 – khelwood
这也许只是措辞不佳而已;他们可能会要求您明确定义一个零参数。 –