我正在通過一個計算機科學論文過去的考試,並在這裏變得有點困惑。構造函數替換默認構造函數?
這些指令是編寫一個類書(完成),它有兩個數據字段(也完成)一個構造函數初始化兩個值(完成以及)和一個構造函數替換默認構造函數(不知道這是什麼是關於)。我已經研究過它,並通過了我的實驗筆記,但我不明白他們在問什麼。
下面的代碼
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
這也許只是措辭不佳而已;他們可能會要求您明確定義一個零參數。 –