我正在完成一项计算机科学论文的过去考试,并且在这里对此部分感到有些困惑。
指令是编写一本类书(完成),它有两个数据字段(也已完成)一个构造函数,它初始化这两个值(也完成)和一个构造函数,它取代了默认的构造函数(不知道这是什么关于)。我已经研究过并通过实验室笔记,但我无法理解他们所要求的内容。
这是代码
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?
答案 0 :(得分:1)
我认为以下几点意味着你已经完成的事情。
1.它有两个数据字段(也已完成)一个初始化这两个值的构造函数 2.constructor替换默认构造函数(不知道这是什么)
默认情况下,class具有默认构造函数。像这样
public Book()
{
}
如果编写一些参数化构造函数,则将替换默认构造函数。通过查看代码,您已经编写了参数化构造函数。
答案 1 :(得分:0)
他们似乎想要这样的事情:
public Book() {
this(0, "");
}