在 Java 中创建构造函数时解决括号错误
resolving bracketing error when creating constructors in Java
当我尝试在以下程序中打开我的第二个构造函数时,我在 Eclipse(第 15 和 18 行)"public Account myCustomAccount ... balance = initial balance; }" 中遇到了括号错误。该程序适用于 Dietel "Introduction to Programming" 第 9 章练习 7。
我怀疑我错误地创建了构造函数。你有什么建议? (提前谢谢你!!)
import java.util.Date;
public class Account {
//declare required variables
private int id = 0;
private double balance = 0;
private double annualInterestRate = 0; //assume all accounts have the same interest rate
private Date dateCreated = new Date(); //no-argument instance stores the present date
//define default & custom constructors
public Account mydefaultaccount = new Account(); //no-argument instance of Account
public Account myCustomAccount = new Account(int identNum, double initialBalance) {
id = identNum;
balance = initialBalance;
}
//define getters
public int getId() {
return id;
}
public double getBalance() {
return balance;
}
public double annualInterestRate() {
return annualInterestRate;
}
public Date getDate() {
return dateCreated;
}
//define setters
public void setId(int idSetter) {
id = idSetter;
}
public void setBalance(double balanceSetter) {
balance = balanceSetter;
}
public void setAnnualInterestRate(double annualSetter) {
annualInterestRate = annualSetter;
}
//define required monthly interest rate getter
public double getMonthlyInterestRate() {
double moInt = annualInterestRate / 12;
return moInt;
}
//define modifiers
public double withdraw(int withdraw) {
balance = balance - withdraw;
}
public double deposit(int deposit) {
balance = balance + deposit;
}
}
这不是您定义构造函数的方式。构造函数应遵循以下形式:
public className(parameters) {}
然后,要实例化 class,请调用:
ClassName variable = new ClassName(Parameters);
在你的情况下,
public Account() {
/* Body */
}
public Account(int identNum, double initialBalance) {
/* Body */
}
并实例化,
Account ac = new Account(Parameters);
当我尝试在以下程序中打开我的第二个构造函数时,我在 Eclipse(第 15 和 18 行)"public Account myCustomAccount ... balance = initial balance; }" 中遇到了括号错误。该程序适用于 Dietel "Introduction to Programming" 第 9 章练习 7。
我怀疑我错误地创建了构造函数。你有什么建议? (提前谢谢你!!)
import java.util.Date;
public class Account {
//declare required variables
private int id = 0;
private double balance = 0;
private double annualInterestRate = 0; //assume all accounts have the same interest rate
private Date dateCreated = new Date(); //no-argument instance stores the present date
//define default & custom constructors
public Account mydefaultaccount = new Account(); //no-argument instance of Account
public Account myCustomAccount = new Account(int identNum, double initialBalance) {
id = identNum;
balance = initialBalance;
}
//define getters
public int getId() {
return id;
}
public double getBalance() {
return balance;
}
public double annualInterestRate() {
return annualInterestRate;
}
public Date getDate() {
return dateCreated;
}
//define setters
public void setId(int idSetter) {
id = idSetter;
}
public void setBalance(double balanceSetter) {
balance = balanceSetter;
}
public void setAnnualInterestRate(double annualSetter) {
annualInterestRate = annualSetter;
}
//define required monthly interest rate getter
public double getMonthlyInterestRate() {
double moInt = annualInterestRate / 12;
return moInt;
}
//define modifiers
public double withdraw(int withdraw) {
balance = balance - withdraw;
}
public double deposit(int deposit) {
balance = balance + deposit;
}
}
这不是您定义构造函数的方式。构造函数应遵循以下形式:
public className(parameters) {}
然后,要实例化 class,请调用:
ClassName variable = new ClassName(Parameters);
在你的情况下,
public Account() {
/* Body */
}
public Account(int identNum, double initialBalance) {
/* Body */
}
并实例化,
Account ac = new Account(Parameters);