我想創建一個沒有參數的構造函數的形式爲我在下面創建的構造函數。我提供了我試圖編寫這個構造函數的嘗試。它是否正確?沒有參數形式的構造函數
/**
* Default Constructor for Testing
*/
public void extractTokens(Scanner scanner)
throws IOException, FileNotFoundException
{
//extracts tokens from the text file
File text = new File("E:/LEWIS BC 2/java project/project 1 part 3/items_all.txt");
String toolName = scanner.next();
String itemCode = scanner.next();
String power = scanner.next();
String timesBorrowed = scanner.next();
String onLoan = scanner.next();
String cost = scanner.next();
String weight = scanner.next();
extractTokens(scanner);
// System.out.println(parts.get(1)); // "en"
}
/**
* Creates a collection of tools to be stored in a tool list
*/
public Shop( String toolName,
int power,
int timesborrowed,
boolean rechargeable,
int itemCode,
int cost,
double weight,
int toolcount,
boolean onLoan )
{
toolsList = new ArrayList<Tool>();
toolName = new String();
power = 0;
timesborrowed = 0;
rechargeable = true;
itemCode = 001;
cost = 100;
weight = 0.0;
toolCount = 0;
onLoan = true;
}
/**
* Default Constructor for Testing
*/
public Shop() {
// initialise instance variables
toolName = "Spanner";
itemCode = 001;
timesBorrowed = 0;
power = 0;
onLoan = true;
rechargeable = true;
itemCode = 001;
cost = 100;
weight = 0.0;
toolCount = 0;
}
顯示實例變量的聲明。 – Christian
您需要在分配值時使用'this'關鍵字。例如'this.toolName =「Spanner」' –
我建議你通過這個東西的基本教程通過你的方式。有許多概念你都無法理解。 –